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