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