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