]> git.sesse.net Git - vlc/blob - plugins/dvdread/input_dvdread.c
b068e41aa1fdcb22afe177d8270be5696fbdfcf6
[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.41 2002/07/23 00:39:17 sam Exp $
10  *
11  * Author: Stéphane Borel <stef@via.ecp.fr>
12  *
13  * Some code taken form the play_title.c by Billy Biggs <vektor@dumbterm.net>
14  * in libdvdread.
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  * 
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
29  *****************************************************************************/
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #include <vlc/vlc.h>
38 #include <vlc/input.h>
39
40 #ifdef HAVE_UNISTD_H
41 #   include <unistd.h>
42 #endif
43
44 #include <fcntl.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <string.h>
48 #include <errno.h>
49 #include <assert.h>
50
51 #ifdef STRNCASECMP_IN_STRINGS_H
52 #   include <strings.h>
53 #endif
54
55 #if defined( WIN32 )
56 #   include <io.h>                                                 /* read() */
57 #else
58 #   include <sys/uio.h>                                      /* struct iovec */
59 #endif
60
61 #if defined( WIN32 )
62 #   include "input_iovec.h"
63 #endif
64
65 #include <dvdread/dvd_reader.h>
66 #include <dvdread/ifo_types.h>
67 #include <dvdread/ifo_read.h>
68 #include <dvdread/nav_read.h>
69 #include <dvdread/nav_print.h>
70
71 #include "input_dvdread.h"
72
73 #include "iso_lang.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     ( input_thread_t * );
83 static void DvdReadEnd      ( input_thread_t * );
84 static int  DvdReadDemux    ( input_thread_t * );
85 static int  DvdReadRewind   ( input_thread_t * );
86
87 static int  DvdReadOpen       ( input_thread_t * );
88 static void DvdReadClose      ( input_thread_t * );
89 static int  DvdReadSetArea    ( input_thread_t *, input_area_t * );
90 static int  DvdReadSetProgram ( input_thread_t *, pgrm_descriptor_t * );
91 static int  DvdReadRead       ( input_thread_t *, byte_t *, size_t );
92 static void DvdReadSeek       ( input_thread_t *, 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( p_input->stream.i_method != INPUT_METHOD_DVD )
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( input_thread_t *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_GetPsz( p_input, "dvd" );
293     }
294
295     if( stat( psz_source, &stat_info ) == -1 )
296     {
297         msg_Err( p_input, "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         msg_Warn( p_input, "dvdread module discarded (not a valid source)" );
306         return -1;
307     }
308     
309     msg_Dbg( p_input, "dvdroot=%s title=%d chapter=%d angle=%d",
310                       psz_source, i_title, i_chapter, i_angle );
311     
312
313     p_dvdread = DVDOpen( psz_source );
314
315     /* free allocated strings */
316     if( psz_source != psz_orig )
317         free( psz_source );
318     free( psz_orig );
319
320     if( ! p_dvdread )
321     {
322         msg_Err( p_input, "libdvdcss cannot open source" );
323         return -1;
324     }
325
326     /* set up input  */
327     p_input->i_mtu = 0;
328
329     p_dvd = malloc( sizeof(thread_dvd_data_t) );
330     if( p_dvd == NULL )
331     {
332         msg_Err( p_input, "out of memory" );
333         return -1;
334     }
335
336     p_dvd->p_dvdread = p_dvdread;
337     p_dvd->p_title = NULL;
338     p_dvd->p_vts_file = NULL;
339
340
341     p_input->p_access_data = (void *)p_dvd;
342
343     /* Ifo allocation & initialisation */
344     if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
345     {
346         msg_Err( p_input, "cannot open VMG info" );
347         free( p_dvd );
348         return -1;
349     }
350     msg_Dbg( p_input, "VMG opened" );
351
352     /* Set stream and area data */
353     vlc_mutex_lock( &p_input->stream.stream_lock );
354
355     p_input->stream.i_method = INPUT_METHOD_DVD;
356
357     /* If we are here we can control the pace... */
358     p_input->stream.b_pace_control = 1;
359     p_input->stream.b_seekable = 1;
360     
361     p_input->stream.p_selected_area->i_size = 0;
362     p_input->stream.p_selected_area->i_tell = 0;
363
364     /* Initialize ES structures */
365     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
366
367     /* disc input method */
368     p_input->stream.i_method = INPUT_METHOD_DVD;
369
370 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
371     msg_Dbg( p_input, "number of titles: %d", tt_srpt->nr_of_srpts );
372
373 #define area p_input->stream.pp_areas
374     /* We start from 1 here since the default area 0
375      * is reserved for video_ts.vob */
376     for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
377     {
378         input_AddArea( p_input );
379
380         /* Titles are Program Chains */
381         area[i]->i_id = i;
382
383         /* Absolute start offset and size
384          * We can only set that with vts ifo, so we do it during the
385          * first call to DVDSetArea */
386         area[i]->i_start = 0;
387         area[i]->i_size = 0;
388
389         /* Number of chapters */
390         area[i]->i_part_nb = tt_srpt->title[i-1].nr_of_ptts;
391         area[i]->i_part = 1;
392
393         area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
394     }
395 #undef area
396
397     p_dvd->i_title = i_title <= tt_srpt->nr_of_srpts ? i_title : 1;
398 #undef tt_srpt
399
400     p_area = p_input->stream.pp_areas[p_dvd->i_title];
401     p_dvd->i_chapter = i_chapter;
402
403     p_dvd->i_chapter = i_chapter < p_area->i_part_nb ? i_chapter : 1;
404     p_area->i_part = p_dvd->i_chapter;
405     
406     p_dvd->i_angle = i_angle;
407
408     /* set title, chapter, audio and subpic */
409     if( DvdReadSetArea( p_input, p_area ) )
410     {
411         vlc_mutex_unlock( &p_input->stream.stream_lock );
412         return -1;
413     }
414
415     vlc_mutex_unlock( &p_input->stream.stream_lock );
416
417     p_input->psz_demux = "dvdread";
418
419     return 0;
420 }
421
422 /*****************************************************************************
423  * DvdReadClose: close libdvdread
424  *****************************************************************************/
425 static void DvdReadClose( input_thread_t *p_input )
426 {
427     thread_dvd_data_t *     p_dvd;
428
429     p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
430
431     /* close libdvdread */
432     DVDCloseFile( p_dvd->p_title );
433     ifoClose( p_dvd->p_vts_file );
434     ifoClose( p_dvd->p_vmg_file );
435
436     DVDClose( p_dvd->p_dvdread );
437     free( p_dvd );
438     p_input->p_access_data = NULL;
439
440 }
441
442 /*****************************************************************************
443  * DvdReadSetProgram: Does nothing, a DVD is mono-program
444  *****************************************************************************/
445 static int DvdReadSetProgram( input_thread_t * p_input,
446                               pgrm_descriptor_t * p_program )
447 {
448     if( p_input->stream.p_selected_program != p_program )
449     {
450         thread_dvd_data_t *  p_dvd;
451     
452         p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);
453         p_dvd->i_angle = p_program->i_number;
454
455         memcpy( p_program, p_input->stream.p_selected_program,
456                 sizeof(pgrm_descriptor_t) );
457         p_program->i_number = p_dvd->i_angle;
458         p_input->stream.p_selected_program = p_program;
459
460         msg_Dbg( p_input, "angle %d selected", p_dvd->i_angle );
461     }
462
463     return 0;
464 }
465
466 #define p_pgc         p_dvd->p_cur_pgc
467
468 /*****************************************************************************
469  * DvdReadSetArea: initialize input data for title x, chapter y.
470  * It should be called for each user navigation request.
471  *****************************************************************************
472  * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
473  * Note that you have to take the lock before entering here.
474  *****************************************************************************/
475 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
476 {
477     thread_dvd_data_t *  p_dvd;
478     int                  pgc_id = 0;
479     int                  pgn = 0;
480
481     p_dvd = (thread_dvd_data_t*)p_input->p_access_data;
482
483     /* we can't use the interface slider until initilization is complete */
484     p_input->stream.b_seekable = 0;
485
486     if( p_area != p_input->stream.p_selected_area )
487     {
488         es_descriptor_t *    p_es;
489         int                  i_cell = 0;
490         int                  i_audio_nb = 0;
491         int                  i_spu_nb = 0;
492         int                  i;
493
494 #define p_vmg         p_dvd->p_vmg_file
495 #define p_vts         p_dvd->p_vts_file
496         if( p_dvd->p_title != NULL )
497         {
498             DVDCloseFile( p_dvd->p_title );
499         }
500
501         if( p_vts != NULL )
502         {
503             ifoClose( p_vts );
504         }
505
506         /* Reset the Chapter position of the old title */
507         p_input->stream.p_selected_area->i_part = 1;
508
509         /*
510          *  We have to load all title information
511          */
512         /* Change the default area */
513         p_input->stream.p_selected_area = p_area;
514
515         msg_Dbg( p_input, "open VTS %d, for title %d",
516             p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
517             p_area->i_id );
518
519         /* ifo vts */
520         if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
521                 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
522         {
523             msg_Err( p_input, "fatal error in vts ifo" );
524             ifoClose( p_vmg );
525             DVDClose( p_dvd->p_dvdread );
526             return -1;
527         }
528
529         /* title position inside the selected vts */
530         p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
531
532         /*
533          * Set selected title start
534          */
535         pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
536         pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
537         p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
538         i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
539
540         p_area->i_start =
541             LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
542
543         msg_Dbg( p_input, "start %d vts_title %d pgc %d pgn %d",
544                   p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
545
546         /*
547          * Find title end
548          */
549         i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
550
551         p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
552         p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
553
554         msg_Dbg( p_input, "start %lld size %lld end %d",
555                   p_area->i_start , p_area->i_size, p_dvd->i_end_block );
556
557         /*
558          * Set properties for current chapter
559          */
560         /* Remeber current chapter */
561         p_dvd->i_chapter = p_area->i_part;
562         p_dvd->b_eoc = 0;
563
564         pgc_id = p_vts->vts_ptt_srpt->title[
565                     p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
566         pgn = p_vts->vts_ptt_srpt->title[
567                     p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
568
569         p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
570         p_dvd->i_pack_len = 0;
571         p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
572         DvdReadFindCell( p_dvd );
573
574         p_dvd->i_next_vobu = p_dvd->i_cur_block =
575             p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
576
577         /*
578          * Angle management
579          */
580         p_dvd->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
581
582         if( p_dvd->i_angle > p_dvd->i_angle_nb )
583         {
584             p_dvd->i_angle = 1;
585         }
586
587         /*
588          * We've got enough info, time to open the title set data.
589          */
590         if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
591             p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
592             DVD_READ_TITLE_VOBS ) ) )
593         {
594             msg_Err( p_input, "cannot open title (VTS_%02d_1.VOB)",
595                      p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
596             ifoClose( p_vts );
597             ifoClose( p_vmg );
598             DVDClose( p_dvd->p_dvdread );
599             return -1;
600         }
601
602 //        IfoPrintTitle( p_dvd );
603
604         /*
605          * Destroy obsolete ES by reinitializing program 0
606          * and find all ES in title with ifo data
607          */
608         if( p_input->stream.pp_programs != NULL )
609         {
610             /* We don't use input_EndStream here since
611              * we keep area structures */
612
613             while( p_input->stream.i_es_number )
614             {
615                 input_DelES( p_input, p_input->stream.pp_es[0] );
616             }
617
618             while( p_input->stream.i_pgrm_number )
619             {
620                 input_DelProgram( p_input, p_input->stream.pp_programs[0] );
621             }
622
623             if( p_input->stream.pp_selected_es )
624             {
625                 free( p_input->stream.pp_selected_es );
626                 p_input->stream.pp_selected_es = NULL;
627             }
628             p_input->stream.i_selected_es_number = 0;
629         }
630
631         input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
632         p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
633
634         for( i = 1 ; i < p_dvd->i_angle_nb ; i++ )
635         {
636             input_AddProgram( p_input, i+1, 0 );
637         }
638         
639         DvdReadSetProgram( p_input,
640                            p_input->stream.pp_programs[p_dvd->i_angle-1] ); 
641
642         /* No PSM to read in DVD mode, we already have all information */
643         p_input->stream.p_selected_program->b_is_ok = 1;
644
645         p_es = NULL;
646
647         /* ES 0 -> video MPEG2 */
648 //        IfoPrintVideo( p_dvd );
649
650         p_es = input_AddES( p_input, NULL, 0xe0, 0 );
651         p_es->i_stream_id = 0xe0;
652         p_es->i_fourcc = VLC_FOURCC('m','p','g','v');
653         p_es->i_cat = VIDEO_ES;
654
655 #define audio_control \
656     p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
657         /* Audio ES, in the order they appear in .ifo */
658         for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
659         {
660             int i_position = 0;
661             u16 i_id;
662
663 //            IfoPrintAudio( p_dvd, i );
664
665             /* audio channel is active if first byte is 0x80 */
666             if( audio_control & 0x8000 )
667             {
668                 i_audio_nb++;
669                 i_position = ( audio_control & 0x7F00 ) >> 8;
670
671             msg_Dbg( p_input, "audio position  %d", i_position );
672                 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
673                 {
674                 case 0x00:              /* AC3 */
675                     i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
676                     p_es = input_AddES( p_input, NULL, i_id, 0 );
677                     p_es->i_stream_id = 0xbd;
678                     p_es->i_fourcc = VLC_FOURCC('a','5','2',' ');
679                     p_es->i_cat = AUDIO_ES;
680                     strcpy( p_es->psz_desc, DecodeLanguage(
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_fourcc = VLC_FOURCC('m','p','g','a');
691                     p_es->i_cat = AUDIO_ES;
692                     strcpy( p_es->psz_desc, DecodeLanguage(
693                         p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ); 
694                     strcat( p_es->psz_desc, " (mpeg)" );
695
696                     break;
697                 case 0x04:              /* LPCM */
698
699                     i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
700                     p_es = input_AddES( p_input, NULL, i_id, 0 );
701                     p_es->i_stream_id = i_id;
702                     p_es->i_fourcc = VLC_FOURCC('l','p','c','m');
703                     p_es->i_cat = AUDIO_ES;
704                     strcpy( p_es->psz_desc, DecodeLanguage(
705                         p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ); 
706                     strcat( p_es->psz_desc, " (lpcm)" );
707
708                     break;
709                 case 0x06:              /* DTS */
710                     i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
711                     msg_Err( p_input, "DTS audio not handled yet"
712                                       "(0x%x)", i_id );
713                     break;
714                 default:
715                     i_id = 0;
716                     msg_Err( p_input, "unknown audio type %.2x",
717                           p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
718                 }
719             }
720         }
721 #undef audio_control
722 #define spu_control \
723     p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
724
725         /* Sub Picture ES */
726
727         for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
728         {
729             int i_position = 0;
730             u16 i_id;
731
732 //            IfoPrintSpu( p_dvd, i );
733             msg_Dbg( p_input, "spu %d 0x%02x", i, spu_control );
734
735             if( spu_control & 0x80000000 )
736             {
737                 i_spu_nb++;
738
739                 /*  there are several streams for one spu */
740                 if(  p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
741                 {
742                     /* 16:9 */
743                     switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
744                     {
745                     case 1:
746                         i_position = spu_control & 0xff;
747                         break;
748                     case 2:
749                         i_position = ( spu_control >> 8 ) & 0xff;
750                         break;
751                     default:
752                         i_position = ( spu_control >> 16 ) & 0xff;
753                         break;
754                     }
755                 }
756                 else
757                 {
758                     /* 4:3 */
759                     i_position = ( spu_control >> 24 ) & 0x7F;
760                 }
761
762                 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
763                 p_es = input_AddES( p_input, NULL, i_id, 0 );
764                 p_es->i_stream_id = 0xbd;
765                 p_es->i_fourcc = VLC_FOURCC('s','p','u',' ');
766                 p_es->i_cat = SPU_ES;
767                 strcpy( p_es->psz_desc, DecodeLanguage(
768                     p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ) ); 
769             }
770         }
771 #undef spu_control
772
773         /* FIXME: hack to check that the demuxer is ready, and set
774          * the decoders */
775         if( p_input->p_demux_module )
776         {
777             DvdReadLauchDecoders( p_input );
778         }
779             
780     } /* i_title >= 0 */
781     else
782     {
783         p_area = p_input->stream.p_selected_area;
784     }
785
786     /*
787      * Chapter selection
788      */
789
790     if( p_area->i_part != p_dvd->i_chapter )
791     {
792         if( ( p_area->i_part > 0 ) &&
793             ( p_area->i_part <= p_area->i_part_nb ))
794         {
795             p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
796             pgc_id = p_vts->vts_ptt_srpt->title[
797                         p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
798             pgn = p_vts->vts_ptt_srpt->title[
799                         p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
800
801             p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
802
803             p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
804             p_dvd->i_chapter = p_area->i_part;
805             DvdReadFindCell( p_dvd );
806
807             p_dvd->i_pack_len = 0;
808             p_dvd->i_next_vobu = p_dvd->i_cur_block =
809                     p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
810         }
811         else
812         {
813             p_area->i_part = p_dvd->i_chapter;
814         }
815     }
816 #undef p_vts
817 #undef p_vmg
818
819     /* warn interface that something has changed */
820     p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
821     p_input->stream.b_seekable = 1;
822     p_input->stream.b_changed = 1;
823
824     return 0;
825 }
826
827
828 /*****************************************************************************
829  * DvdReadRead: reads data packets into the netlist.
830  *****************************************************************************
831  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
832  * EOF.
833  *****************************************************************************/
834 static int DvdReadRead( input_thread_t * p_input,
835                         byte_t * p_buffer, size_t i_count )
836 {
837     thread_dvd_data_t *     p_dvd;
838     byte_t *                p_buf;
839     int                     i_blocks_once;
840     int                     i_blocks;
841     int                     i_read;
842     int                     i_read_total;
843     vlc_bool_t              b_eot = 0;
844
845     p_dvd = (thread_dvd_data_t *)p_input->p_access_data;
846     p_buf = p_buffer;
847
848     /*
849      * Playback by cell in this pgc, starting at the cell for our chapter.
850      */
851     i_blocks = OFF2LB( i_count );
852     i_read_total = 0;
853     i_read = 0;
854
855     while( i_blocks )
856     {
857         /* 
858          * End of pack, we select the following one
859          */
860         if( ! p_dvd->i_pack_len )
861         {
862             /*
863              * Read NAV packet.
864              */
865             if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
866                            1, p_buf ) ) != 1 )
867             {
868                 msg_Err( p_input, "read failed for block %d",
869                                   p_dvd->i_next_vobu );
870                 return -1;
871             }
872
873             /* basic check to be sure we don't have a empty title
874              * go to next title if so */
875             //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
876             
877             /*
878              * Parse the contained dsi packet.
879              */
880
881             DvdReadHandleDSI( p_dvd, p_buf );
882
883             /* End of File */
884             if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
885             {
886                 return 1;
887             }
888
889             assert( p_dvd->i_pack_len < 1024 );
890             /* FIXME: Ugly kludge: we send the pack block to the input for it
891              * sometimes has a zero scr and restart the sync */
892             p_dvd->i_cur_block ++;
893             //p_dvd->i_pack_len++;
894
895             i_read_total++;
896             p_buf += DVD_VIDEO_LB_LEN;
897             i_blocks--;
898         }
899
900         /*
901          * Compute the number of blocks to read
902          */
903         i_blocks_once = p_dvd->i_pack_len >= i_blocks
904                  ? i_blocks : p_dvd->i_pack_len;
905         p_dvd->i_pack_len -= i_blocks_once;
906
907         /* Reads from DVD */
908         i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_cur_block,
909                                 i_blocks_once, p_buf );
910         if( i_read != i_blocks_once )
911         {
912             msg_Err( p_input, "read failed for %d/%d blocks at 0x%02x",
913                               i_read, i_blocks_once, p_dvd->i_cur_block );
914             return -1;
915         }
916
917         i_blocks -= i_read;
918         i_read_total += i_read;
919         p_dvd->i_cur_block += i_read;
920         p_buf += LB2OFF( i_read );
921
922     }
923 /*
924     msg_Dbg( p_input, "i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
925 */
926
927     vlc_mutex_lock( &p_input->stream.stream_lock );
928
929     p_input->stream.p_selected_area->i_tell =
930         LB2OFF( p_dvd->i_cur_block ) -
931             p_input->stream.p_selected_area->i_start;
932
933     if( p_dvd->b_eoc )
934     {
935         /* We modify i_part only at end of chapter not to erase
936          * some modification from the interface */
937         p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
938         p_dvd->b_eoc = 0;
939     }
940     
941     if( p_input->stream.p_selected_area->i_tell
942             >= p_input->stream.p_selected_area->i_size || b_eot )
943     {
944         if( ( p_input->stream.p_selected_area->i_id + 1 ) >= 
945                         p_input->stream.i_area_nb )
946         {
947             /* EOF */
948             vlc_mutex_unlock( &p_input->stream.stream_lock );
949             return 1;
950         }
951
952         /* EOT */
953         msg_Dbg( p_input, "new title" );
954         DvdReadSetArea( p_input, p_input->stream.pp_areas[
955                         p_input->stream.p_selected_area->i_id+1] );
956         vlc_mutex_unlock( &p_input->stream.stream_lock );
957         return 0;
958     }
959
960     vlc_mutex_unlock( &p_input->stream.stream_lock );
961
962     return LB2OFF( i_read_total );
963 }
964 #undef p_pgc
965
966 /*****************************************************************************
967  * DvdReadSeek : Goes to a given position on the stream.
968  *****************************************************************************
969  * This one is used by the input and translate chronological position from
970  * input to logical position on the device.
971  * The lock should be taken before calling this function.
972  *****************************************************************************/
973 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
974 {
975     thread_dvd_data_t *     p_dvd;
976     int                     i_lb;
977     int                     i_tmp;
978     int                     i_chapter = 0;
979     int                     i_cell = 0;
980     int                     i_vobu = 0;
981     int                     i_sub_cell = 0;
982
983     vlc_mutex_lock( &p_input->stream.stream_lock );
984     i_off += p_input->stream.p_selected_area->i_start;
985     vlc_mutex_unlock( &p_input->stream.stream_lock );
986     
987     i_lb = OFF2LB( i_off );
988     p_dvd = ( thread_dvd_data_t * )p_input->p_access_data;
989
990     /* find cell */
991     while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
992     {
993         i_cell++;
994     }
995
996     /* find chapter */
997     do
998     {
999         pgc_t *     p_pgc;
1000         int         pgc_id, pgn;
1001
1002         i_chapter++;
1003         pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1004                     p_dvd->i_ttn-1].ptt[i_chapter-1].pgcn;
1005         pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1006                     p_dvd->i_ttn-1].ptt[i_chapter-1].pgn;
1007
1008         p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1009         i_tmp = p_pgc->program_map[pgn-1];
1010
1011     } while( i_tmp <= i_cell );
1012
1013     /* find vobu */
1014     while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
1015             <= i_lb )
1016     {
1017         i_vobu++;
1018     }
1019
1020     /* find sub_cell */
1021     while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1022             p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1023     {
1024         i_sub_cell++;
1025     }
1026
1027 /*
1028     msg_Dbg( p_input, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1029             i_cell, i_sub_cell,i_chapter, i_vobu,
1030             p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1031             p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1032             p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1033 */
1034     p_dvd->i_cur_block = i_lb;
1035     p_dvd->i_next_vobu =
1036         p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1037     p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1038     p_dvd->i_cur_cell = i_cell;
1039     p_dvd->i_chapter = i_chapter;
1040     DvdReadFindCell( p_dvd );
1041
1042     vlc_mutex_lock( &p_input->stream.stream_lock );
1043     p_input->stream.p_selected_area->i_tell =
1044         LB2OFF ( p_dvd->i_cur_block )
1045          - p_input->stream.p_selected_area->i_start;
1046     p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1047     vlc_mutex_unlock( &p_input->stream.stream_lock );
1048
1049     return;
1050 }
1051
1052 /*****************************************************************************
1053  * DvdReadHandleDSI
1054  *****************************************************************************/
1055 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data )
1056 {
1057     navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1058
1059     /*
1060      * Determine where we go next.  These values are the ones we mostly
1061      * care about.
1062      */
1063     p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1064
1065     /*
1066      * If we're not at the end of this cell, we can determine the next
1067      * VOBU to display using the VOBU_SRI information section of the
1068      * DSI.  Using this value correctly follows the current angle,
1069      * avoiding the doubled scenes in The Matrix, and makes our life
1070      * really happy.
1071      */
1072     if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1073     {
1074 #if 1
1075         switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1076         {
1077             case 0x4:
1078                 /* interleaved unit with no angle */
1079                 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != -1 )
1080                 {
1081                     p_dvd->i_next_vobu = p_dvd->i_cur_block +
1082                         p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1083                     p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1084                 }
1085                 else
1086                 {
1087                     p_dvd->i_next_vobu = p_dvd->i_cur_block +
1088                         p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1089                     p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1090                 }
1091                 break;
1092             case 0x5:
1093                 /* vobu is end of ilvu */
1094                 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1095                 {
1096                     p_dvd->i_next_vobu = p_dvd->i_cur_block +
1097                         p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1098                     p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1099
1100                     break;
1101                 }
1102             case 0x6:
1103                 /* vobu is beginning of ilvu */
1104             case 0x9:
1105                 /* next scr is 0 */
1106             case 0xa:
1107                 /* entering interleaved section */
1108             case 0x8:
1109                 /* non interleaved cells in interleaved section */
1110             default:
1111                 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1112                     ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1113                 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1114                 break;
1115         }
1116 #else
1117         p_dvd->i_next_vobu = p_dvd->i_cur_block +
1118             ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1119         p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1120 #endif
1121     }
1122     else
1123     {
1124         p_dvd->i_cur_cell = p_dvd->i_next_cell;
1125         DvdReadFindCell( p_dvd );
1126
1127         p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1128         p_dvd->i_next_vobu =
1129             p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1130     }
1131
1132 #if 0
1133     msg_Dbg( p_input, 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1134              p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1135              p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1136              p_dvd->dsi_pack.dsi_gi.vobu_ea,
1137              p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1138              p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1139
1140     msg_Dbg( p_input, 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d", 
1141              p_dvd->dsi_pack.sml_pbi.category,
1142              p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1143              p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1144              p_dvd->dsi_pack.sml_pbi.size );
1145
1146     msg_Dbg( p_input, 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1147              p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1148              p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1149              p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1150 #endif
1151 }
1152
1153 /*****************************************************************************
1154  * DvdReadFindCell
1155  *****************************************************************************/
1156 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1157 {
1158     int         pgc_id, pgn;
1159     int         i = 0;
1160     pgc_t *     p_pgc;
1161 #define cell p_dvd->p_cur_pgc->cell_playback
1162     if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1163     {
1164 #if 0
1165         p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1166         p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1167 #else
1168         p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1169
1170         while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1171         {
1172             i++;
1173         }
1174         p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1175 #endif
1176     }
1177     else
1178     {
1179         p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1180     }
1181 #undef cell
1182     pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1183                 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgcn;
1184     pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1185                 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgn;
1186     p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1187
1188     if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )
1189     {
1190         p_dvd->i_chapter++;
1191         p_dvd->b_eoc = 1;
1192     }
1193 }
1194
1195 /*****************************************************************************
1196  * DvdReadLaunchDecoders
1197  *****************************************************************************/
1198 static void DvdReadLauchDecoders( input_thread_t * p_input )
1199 {
1200     thread_dvd_data_t *  p_dvd;
1201     
1202     p_dvd = (thread_dvd_data_t*)(p_input->p_access_data);            
1203             
1204     if( config_GetInt( p_input, "video" ) )
1205     {
1206         input_SelectES( p_input, p_input->stream.pp_es[0] );
1207     }
1208
1209     if( config_GetInt( p_input, "audio" ) )
1210     {
1211         /* For audio: first one if none or a not existing one specified */
1212         int i_audio = config_GetInt( p_input, "audio-channel" );
1213         if( i_audio < 0 /*|| i_audio > i_audio_nb*/ )
1214         {
1215             config_PutInt( p_input, "audio-channel", 1 );
1216             i_audio = 1;
1217         }
1218         if( i_audio > 0/* && i_audio_nb > 0*/ )
1219         {
1220             if( config_GetInt( p_input, "audio-type" )
1221                  == REQUESTED_AC3 )
1222             {
1223                 int     i_ac3 = i_audio;
1224                 while( ( p_input->stream.pp_es[i_ac3]->i_fourcc !=
1225                        VLC_FOURCC('a','5','2',' ') ) && ( i_ac3 <=
1226                        p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams ) )
1227                 {
1228                     i_ac3++;
1229                 }
1230                 if( p_input->stream.pp_es[i_ac3]->i_fourcc
1231                      == VLC_FOURCC('a','5','2',' ') )
1232                 {
1233                     input_SelectES( p_input,
1234                                     p_input->stream.pp_es[i_ac3] );
1235                 }
1236             }
1237             else
1238             {
1239                 input_SelectES( p_input,
1240                                 p_input->stream.pp_es[i_audio] );
1241             }
1242         }
1243     }
1244
1245     if( config_GetInt( p_input, "video" ) )
1246     {
1247         /* for spu, default is none */
1248         int i_spu = config_GetInt( p_input, "spu-channel" );
1249         if( i_spu < 0 /*|| i_spu > i_spu_nb*/ )
1250         {
1251             config_PutInt( p_input, "spu-channel", 0 );
1252             i_spu = 0;
1253         }
1254         if( i_spu > 0 /*&& i_spu_nb > 0*/ )
1255         {
1256             i_spu += p_dvd->p_vts_file->vtsi_mat->nr_of_vts_audio_streams;
1257             input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
1258         }
1259     }
1260 }