]> git.sesse.net Git - vlc/blob - plugins/dvd/input_dvd.c
Miscellaneous typos.
[vlc] / plugins / dvd / input_dvd.c
1 /*****************************************************************************
2  * input_dvd.c: DVD raw reading 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:
7  *  -libdvdcss for access and unscrambling
8  *  -dvd_ifo for ifo parsing and analyse
9  *  -dvd_udf to find files
10  *****************************************************************************
11  * Copyright (C) 1998-2001 VideoLAN
12  * $Id: input_dvd.c,v 1.117 2001/12/31 01:13:12 massiot Exp $
13  *
14  * Author: Stéphane Borel <stef@via.ecp.fr>
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 #include <string.h>
37
38 #include <videolan/vlc.h>
39
40 #ifdef HAVE_UNISTD_H
41 #   include <unistd.h>
42 #endif
43
44 #include <fcntl.h>
45 #include <sys/types.h>
46 #include <string.h>
47 #include <errno.h>
48
49 #ifdef STRNCASECMP_IN_STRINGS_H
50 #   include <strings.h>
51 #endif
52
53 #if defined( WIN32 )
54 #   include <io.h>                                                 /* read() */
55 #else
56 #   include <sys/uio.h>                                      /* struct iovec */
57 #endif
58
59 #ifdef GOD_DAMN_DMCA
60 #   include "dummy_dvdcss.h"
61 #else
62 #   include <videolan/dvdcss.h>
63 #endif
64
65 #if defined( WIN32 )
66 #   include "input_iovec.h"
67 #endif
68
69 #include "stream_control.h"
70 #include "input_ext-intf.h"
71 #include "input_ext-dec.h"
72 #include "input_ext-plugins.h"
73
74 #include "input_dvd.h"
75 #include "dvd_ifo.h"
76 #include "dvd_summary.h"
77 #include "iso_lang.h"
78
79 #include "debug.h"
80
81 /* how many blocks DVDRead will read in each loop */
82 #define DVD_BLOCK_READ_ONCE 64
83
84 /*****************************************************************************
85  * Local prototypes
86  *****************************************************************************/
87 /* called from outside */
88 static int  DVDProbe        ( probedata_t *p_data );
89 static void DVDInit         ( struct input_thread_s * );
90 static void DVDEnd          ( struct input_thread_s * );
91 static void DVDOpen         ( struct input_thread_s * );
92 static void DVDClose        ( struct input_thread_s * );
93 static int  DVDSetArea      ( struct input_thread_s *, struct input_area_s * );
94 static int  DVDSetProgram   ( struct input_thread_s *, pgrm_descriptor_t * );
95 static int  DVDRead         ( struct input_thread_s *, data_packet_t ** );
96 static void DVDSeek         ( struct input_thread_s *, off_t );
97 static int  DVDRewind       ( struct input_thread_s * );
98
99 /* called only inside */
100 static int  DVDChooseAngle( thread_dvd_data_t * );
101 static int  DVDFindCell( thread_dvd_data_t * );
102 static int  DVDFindSector( thread_dvd_data_t * );
103 static int  DVDChapterSelect( thread_dvd_data_t *, int );
104
105 /*****************************************************************************
106  * Declare a buffer manager
107  *****************************************************************************/
108 #define FLAGS           BUFFERS_UNIQUE_SIZE
109 #define NB_LIFO         1
110 DECLARE_BUFFERS_SHARED( FLAGS, NB_LIFO );
111 DECLARE_BUFFERS_INIT( FLAGS, NB_LIFO );
112 DECLARE_BUFFERS_END_SHARED( FLAGS, NB_LIFO );
113 DECLARE_BUFFERS_NEWPACKET_SHARED( FLAGS, NB_LIFO );
114 DECLARE_BUFFERS_DELETEPACKET_SHARED( FLAGS, NB_LIFO, 1000 );
115 DECLARE_BUFFERS_NEWPES( FLAGS, NB_LIFO );
116 DECLARE_BUFFERS_DELETEPES( FLAGS, NB_LIFO, 1000 );
117 DECLARE_BUFFERS_TOIO( FLAGS, DVD_LB_SIZE );
118 DECLARE_BUFFERS_SHAREBUFFER( FLAGS );
119
120 /*****************************************************************************
121  * Functions exported as capabilities. They are declared as static so that
122  * we don't pollute the namespace too much.
123  *****************************************************************************/
124 void _M( input_getfunctions )( function_list_t * p_function_list )
125 {
126 #define input p_function_list->functions.input
127     p_function_list->pf_probe = DVDProbe;
128     input.pf_init             = DVDInit;
129     input.pf_open             = DVDOpen;
130     input.pf_close            = DVDClose;
131     input.pf_end              = DVDEnd;
132     input.pf_init_bit_stream  = InitBitstream;
133     input.pf_read             = DVDRead;
134     input.pf_set_area         = DVDSetArea;
135     input.pf_set_program      = DVDSetProgram;
136     input.pf_demux            = input_DemuxPS;
137     input.pf_new_packet       = input_NewPacket;
138     input.pf_new_pes          = input_NewPES;
139     input.pf_delete_packet    = input_DeletePacket;
140     input.pf_delete_pes       = input_DeletePES;
141     input.pf_rewind           = DVDRewind;
142     input.pf_seek             = DVDSeek;
143 #undef input
144 }
145
146 /*
147  * Data reading functions
148  */
149
150 /*****************************************************************************
151  * DVDProbe: verifies that the stream is a PS stream
152  *****************************************************************************/
153 static int DVDProbe( probedata_t *p_data )
154 {
155     input_thread_t * p_input = (input_thread_t *)p_data;
156
157     char * psz_name = p_input->p_source;
158     int i_score = 0;
159
160     if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
161     {
162         /* If the user specified "dvd:" then it's probably a DVD */
163         i_score = 100;
164         psz_name += 4;
165     }
166
167     return( i_score );
168 }
169
170 /*****************************************************************************
171  * DVDInit: initializes DVD structures
172  *****************************************************************************/
173 static void DVDInit( input_thread_t * p_input )
174 {
175     thread_dvd_data_t *  p_dvd;
176     input_area_t *       p_area;
177     int                  i_title;
178     int                  i_chapter;
179     int                  i;
180
181     p_dvd = malloc( sizeof(thread_dvd_data_t) );
182     if( p_dvd == NULL )
183     {
184         intf_ErrMsg( "dvd error: out of memory" );
185         p_input->b_error = 1;
186         return;
187     }
188
189     p_input->p_plugin_data = (void *)p_dvd;
190
191     if( (p_input->p_method_data = input_BuffersInit()) == NULL )
192     {
193         p_input->b_error = 1;
194         return;
195     }
196
197     p_dvd->dvdhandle = (dvdcss_handle) p_input->p_handle;
198
199     if( dvdcss_seek( p_dvd->dvdhandle, 0, DVDCSS_NOFLAGS ) < 0 )
200     {
201         intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
202         input_BuffersEnd( p_input->p_method_data );
203         p_input->b_error = 1;
204         return;
205     }
206
207     /* We read DVD_BLOCK_READ_ONCE in each loop */
208     p_dvd->i_block_once = DVD_BLOCK_READ_ONCE;
209
210     /* Ifo allocation & initialisation */
211     if( IfoCreate( p_dvd ) < 0 )
212     {
213         intf_ErrMsg( "dvd error: allcation error in ifo" );
214         free( p_dvd );
215         input_BuffersEnd( p_input->p_method_data );
216         p_input->b_error = 1;
217         return;
218     }
219
220     if( IfoInit( p_dvd->p_ifo ) < 0 )
221     {
222         intf_ErrMsg( "dvd error: fatal failure in ifo" );
223         IfoDestroy( p_dvd->p_ifo );
224         free( p_dvd );
225         input_BuffersEnd( p_input->p_method_data );
226         p_input->b_error = 1;
227         return;
228     }
229
230     /* Set stream and area data */
231     vlc_mutex_lock( &p_input->stream.stream_lock );
232
233     /* Initialize ES structures */
234     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
235
236 #define title_inf p_dvd->p_ifo->vmg.title_inf
237     intf_WarnMsg( 2, "dvd info: number of titles: %d", title_inf.i_title_nb );
238
239 #define area p_input->stream.pp_areas
240     /* We start from 1 here since the default area 0
241      * is reserved for video_ts.vob */
242     for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
243     {
244         input_AddArea( p_input );
245
246         /* Titles are Program Chains */
247         area[i]->i_id = i;
248
249         /* Absolute start offset and size 
250          * We can only set that with vts ifo, so we do it during the
251          * first call to DVDSetArea */
252         area[i]->i_start = 0;
253         area[i]->i_size = 0;
254
255         /* Number of chapters */
256         area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
257         area[i]->i_part = 1;
258
259         /* Number of angles */
260         area[i]->i_angle_nb = 0;
261         area[i]->i_angle = 1;
262
263         /* Offset to vts_i_0.ifo */
264         area[i]->i_plugin_data = p_dvd->p_ifo->i_start +
265                        title_inf.p_attr[i-1].i_start_sector;
266     }   
267 #undef area
268
269     /* Get requested title - if none try the first title */
270     i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
271     if( i_title <= 0 || i_title > title_inf.i_title_nb )
272     {
273         i_title = 1;
274     }
275
276 #undef title_inf
277
278     /* Get requested chapter - if none defaults to first one */
279     i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
280     if( i_chapter <= 0 )
281     {
282         i_chapter = 1;
283     }
284
285     p_area = p_input->stream.pp_areas[i_title];
286     p_area->i_part = i_chapter;
287
288     /* set title, chapter, audio and subpic */
289     DVDSetArea( p_input, p_area );
290
291     vlc_mutex_unlock( &p_input->stream.stream_lock );
292
293     return;
294 }
295
296 /*****************************************************************************
297  * DVDOpen: open dvd
298  *****************************************************************************/
299 static void DVDOpen( struct input_thread_s *p_input )
300 {
301     dvdcss_handle dvdhandle;
302     char * psz_parser;
303     char * psz_device;
304
305     vlc_mutex_lock( &p_input->stream.stream_lock );
306
307     /* If we are here we can control the pace... */
308     p_input->stream.b_pace_control = 1;
309
310     p_input->stream.b_seekable = 1;
311     p_input->stream.p_selected_area->i_size = 0;
312
313     p_input->stream.p_selected_area->i_tell = 0;
314
315     vlc_mutex_unlock( &p_input->stream.stream_lock );
316
317     /* Parse input string : dvd:device[@rawdevice] */
318     if( strlen( p_input->p_source ) > 4
319          && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
320     {
321         psz_parser = psz_device = p_input->p_source + 4;
322     }
323     else
324     {
325         psz_parser = psz_device = p_input->p_source;
326     }
327
328     while( *psz_parser && *psz_parser != '@' )
329     {
330         psz_parser++;
331     }
332
333     if( *psz_parser == '@' )
334     {
335         /* Found raw device */
336         *psz_parser = '\0';
337         psz_parser++;
338
339         main_PutPszVariable( "DVDCSS_RAW_DEVICE", psz_parser );
340     }
341
342     intf_WarnMsg( 2, "input: dvd=%s raw=%s", psz_device, psz_parser );
343
344     dvdhandle = dvdcss_open( psz_device );
345
346     if( dvdhandle == NULL )
347     {
348         intf_ErrMsg( "dvd error: dvdcss can't open device" );
349         p_input->b_error = 1;
350         return;
351     }
352
353     p_input->p_handle = (void *) dvdhandle;
354 }
355
356 /*****************************************************************************
357  * DVDClose: close dvd
358  *****************************************************************************/
359 static void DVDClose( struct input_thread_s *p_input )
360 {
361     /* Clean up libdvdcss */
362     dvdcss_close( (dvdcss_handle) p_input->p_handle );
363 }
364
365 /*****************************************************************************
366  * DVDEnd: frees unused data
367  *****************************************************************************/
368 static void DVDEnd( input_thread_t * p_input )
369 {
370     thread_dvd_data_t *     p_dvd;
371
372     p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
373
374     IfoDestroy( p_dvd->p_ifo );
375
376     free( p_dvd );
377
378     input_BuffersEnd( p_input->p_method_data );
379 }
380
381 /*****************************************************************************
382  * DVDSetProgram: Does nothing, a DVD is mono-program
383  *****************************************************************************/
384 static int DVDSetProgram( input_thread_t * p_input, 
385             pgrm_descriptor_t * p_program ) 
386 {
387     return 0;
388 }
389
390 /*****************************************************************************
391  * DVDSetArea: initialize input data for title x, chapter y.
392  * It should be called for each user navigation request.
393  *****************************************************************************
394  * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
395  * Note that you have to take the lock before entering here.
396  *****************************************************************************/
397 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
398 {
399     thread_dvd_data_t *  p_dvd;
400     es_descriptor_t *    p_es;
401     u16                  i_id;
402     int                  i_vts_title;
403     int                  i_audio_nb = 0;
404     int                  i_spu_nb = 0;
405     int                  i_audio;
406     int                  i_spu;
407     int                  i;
408
409     p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
410
411     /* we can't use the interface slider until initilization is complete */
412     p_input->stream.b_seekable = 0;
413
414     if( p_area != p_input->stream.p_selected_area )
415     {
416         /* Reset the Chapter position of the old title */
417         p_input->stream.p_selected_area->i_part = 0;
418
419         /*
420          *  We have to load all title information
421          */
422         /* Change the default area */
423         p_input->stream.p_selected_area =
424                     p_input->stream.pp_areas[p_area->i_id];
425
426         /* title number: it is not vts nb!,
427          * it is what appears in the interface list */
428         p_dvd->i_title = p_area->i_id;
429         p_dvd->p_ifo->i_title = p_dvd->i_title;
430
431         /* set number of chapters of current title */
432         p_dvd->i_chapter_nb = p_area->i_part_nb;
433
434         /* ifo vts */
435         if( IfoTitleSet( p_dvd->p_ifo ) < 0 )
436         {
437             intf_ErrMsg( "dvd error: fatal error in vts ifo" );
438             free( p_dvd );
439             p_input->b_error = 1;
440             return -1;
441         }
442
443 #define vmg p_dvd->p_ifo->vmg
444 #define vts p_dvd->p_ifo->vts
445         /* title position inside the selected vts */
446         i_vts_title = vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
447         p_dvd->i_title_id =
448             vts.title_inf.p_title_start[i_vts_title-1].i_title_id;
449
450         intf_WarnMsgImm( 3, "dvd: title %d vts_title %d pgc %d",
451                          p_dvd->i_title, i_vts_title, p_dvd->i_title_id );
452
453
454         /*
455          * Angle management
456          */
457         p_dvd->i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
458         p_dvd->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
459         if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
460         {
461             p_dvd->i_angle = 1;
462         }
463     
464         /*
465          * Set selected title start and size
466          */
467         
468         /* title set offset XXX: convert to block values */
469         p_dvd->i_title_start =
470             vts.i_pos + vts.manager_inf.i_title_vob_start_sector;
471
472         /* last video cell */
473         p_dvd->i_cell = 0;
474         p_dvd->i_prg_cell = -1 +
475             vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
476
477         if( DVDFindCell( p_dvd ) < 0 )
478         {
479             intf_ErrMsg( "dvd error: can't find title end" );
480             p_input->b_error = 1;
481             return -1;
482         }
483         
484         /* temporary hack to fix size in some dvds */
485         if( p_dvd->i_cell >= vts.cell_inf.i_cell_nb )
486         {
487             p_dvd->i_cell = vts.cell_inf.i_cell_nb - 1;
488         }
489
490         p_dvd->i_sector = 0;
491         p_dvd->i_size = vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector;
492
493         if( DVDChapterSelect( p_dvd, 1 ) < 0 )
494         {
495             intf_ErrMsg( "dvd error: can't find first chapter" );
496             p_input->b_error = 1;
497             return -1;
498         }
499         
500         /* Force libdvdcss to check its title key.
501          * It is only useful for title cracking method. Methods using the
502          * decrypted disc key are fast enough to check the key at each seek */
503
504         if( dvdcss_seek( p_dvd->dvdhandle, p_dvd->i_start,
505                             DVDCSS_SEEK_KEY ) < 0 )
506         {
507             intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
508             return -1;
509         }
510
511         p_dvd->i_size -= p_dvd->i_sector + 1;
512
513         IfoPrintTitle( p_dvd );
514
515         /* Area definition */
516         p_input->stream.p_selected_area->i_start = LB2OFF( p_dvd->i_start );
517         p_input->stream.p_selected_area->i_size = LB2OFF( p_dvd->i_size );
518         p_input->stream.p_selected_area->i_angle_nb = p_dvd->i_angle_nb;
519         p_input->stream.p_selected_area->i_angle = p_dvd->i_angle;
520
521 #if 0
522         /* start at the beginning of the title */
523         /* FIXME: create a conf option to select whether to restart
524          * title or not */
525         p_input->stream.p_selected_area->i_tell = 0;
526         p_input->stream.p_selected_area->i_part = 1;
527 #endif
528
529         /*
530          * Destroy obsolete ES by reinitializing program 0
531          * and find all ES in title with ifo data
532          */
533         if( p_input->stream.pp_programs != NULL )
534         {
535             /* We don't use input_EndStream here since
536              * we keep area structures */
537
538             for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
539             {
540                 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
541             }
542
543             free( p_input->stream.pp_selected_es );
544             input_DelProgram( p_input, p_input->stream.p_selected_program );
545
546             p_input->stream.pp_selected_es = NULL;
547             p_input->stream.i_selected_es_number = 0;
548         }
549
550         input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
551         p_input->stream.p_selected_program = p_input->stream.pp_programs[0]; 
552
553         /* No PSM to read in DVD mode, we already have all information */
554         p_input->stream.p_selected_program->b_is_ok = 1;
555
556         p_es = NULL;
557
558         /* ES 0 -> video MPEG2 */
559         IfoPrintVideo( p_dvd );
560
561         p_es = input_AddES( p_input, p_input->stream.p_selected_program, 
562                 0xe0, 0 );
563         p_es->i_stream_id = 0xe0;
564         p_es->i_type = MPEG2_VIDEO_ES;
565         p_es->i_cat = VIDEO_ES;
566         if( p_main->b_video )
567         {
568             input_SelectES( p_input, p_es );
569         }
570
571 #define audio_status \
572     vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_audio_status[i-1]
573         /* Audio ES, in the order they appear in .ifo */
574         for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
575         {
576             IfoPrintAudio( p_dvd, i );
577
578             /* audio channel is active if first byte is 0x80 */
579             if( audio_status.i_available )
580             {
581                 i_audio_nb++;
582
583                 switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
584                 {
585                 case 0x00:              /* AC3 */
586                     i_id = ( ( 0x80 + audio_status.i_position ) << 8 ) | 0xbd;
587                     p_es = input_AddES( p_input,
588                                p_input->stream.p_selected_program, i_id, 0 );
589                     p_es->i_stream_id = 0xbd;
590                     p_es->i_type = AC3_AUDIO_ES;
591                     p_es->b_audio = 1;
592                     p_es->i_cat = AUDIO_ES;
593                     strcpy( p_es->psz_desc, DecodeLanguage( hton16(
594                         vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
595                     strcat( p_es->psz_desc, " (ac3)" );
596     
597                     break;
598                 case 0x02:
599                 case 0x03:              /* MPEG audio */
600                     i_id = 0xc0 + audio_status.i_position;
601                     p_es = input_AddES( p_input,
602                                     p_input->stream.p_selected_program, i_id
603                                     , 0 );
604                     p_es->i_stream_id = i_id;
605                     p_es->i_type = MPEG2_AUDIO_ES;
606                     p_es->b_audio = 1;
607                     p_es->i_cat = AUDIO_ES;
608                     strcpy( p_es->psz_desc, DecodeLanguage( hton16(
609                         vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
610                     strcat( p_es->psz_desc, " (mpeg)" );
611     
612                     break;
613                 case 0x04:              /* LPCM */
614     
615                     i_id = ( ( 0xa0 + audio_status.i_position ) << 8 ) | 0xbd;
616                     p_es = input_AddES( p_input,
617                                     p_input->stream.p_selected_program,
618                                     i_id, 0 );
619                     p_es->i_stream_id = 0xbd;
620                     p_es->i_type = LPCM_AUDIO_ES;
621                     p_es->b_audio = 1;
622                     p_es->i_cat = AUDIO_ES;
623                     strcpy( p_es->psz_desc, DecodeLanguage( hton16(
624                         vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
625                     strcat( p_es->psz_desc, " (lpcm)" );
626     
627                     break;
628                 case 0x06:              /* DTS */
629                     i_id = ( ( 0x88 + audio_status.i_position ) << 8 ) | 0xbd;
630                     intf_ErrMsg( "dvd warning: DTS audio not handled yet"
631                                  "(0x%x)", i_id );
632                     break;
633                 default:
634                     i_id = 0;
635                     intf_ErrMsg( "dvd warning: unknown audio type %.2x",
636                              vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
637                 }
638             }
639         }
640 #undef audio_status
641 #define spu_status \
642     vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_spu_status[i-1]
643
644         /* Sub Picture ES */
645            
646         for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
647         {
648             IfoPrintSpu( p_dvd, i );
649
650             if( spu_status.i_available )
651             {
652                 i_spu_nb++;
653
654                 /*  there are several streams for one spu */
655                 if(  vts.manager_inf.video_attr.i_ratio )
656                 {
657                     /* 16:9 */
658                     switch( vts.manager_inf.video_attr.i_perm_displ )
659                     {
660                     case 1:
661                         i_id = ( ( 0x20 + spu_status.i_position_pan ) << 8 )
662                                | 0xbd;
663                         break;
664                     case 2:
665                         i_id = ( ( 0x20 + spu_status.i_position_letter ) << 8 )
666                                | 0xbd;
667                         break;
668                     default:
669                         i_id = ( ( 0x20 + spu_status.i_position_wide ) << 8 )
670                                | 0xbd;
671                         break;
672                     }
673                 }
674                 else
675                 {
676                     /* 4:3 */
677                     i_id = ( ( 0x20 + spu_status.i_position_43 ) << 8 )
678                            | 0xbd;
679                 }
680                 p_es = input_AddES( p_input,
681                                     p_input->stream.p_selected_program,
682                                     i_id, 0 );
683                 p_es->i_stream_id = 0xbd;
684                 p_es->i_type = DVD_SPU_ES;
685                 p_es->i_cat = SPU_ES;
686                 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
687                     vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) ); 
688             }
689         }
690 #undef spu_status
691         if( p_main->b_audio )
692         {
693             /* For audio: first one if none or a not existing one specified */
694             i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
695             if( i_audio < 0 || i_audio > i_audio_nb )
696             {
697                 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
698                 i_audio = 1;
699             }
700             if( i_audio > 0 && i_audio_nb > 0 )
701             {
702                 if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) ||
703                     ( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) ==
704                       REQUESTED_AC3 ) )
705                 {
706                     int     i_ac3 = i_audio;
707                     while( ( p_input->stream.pp_es[i_ac3]->i_type !=
708                              AC3_AUDIO_ES ) && ( i_ac3 <=
709                              vts.manager_inf.i_audio_nb ) )
710                     {
711                         i_ac3++;
712                     }
713                     if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
714                     {
715                         input_SelectES( p_input,
716                                         p_input->stream.pp_es[i_ac3] );
717                     }
718                 }
719                 else
720                 {
721                     input_SelectES( p_input,
722                                     p_input->stream.pp_es[i_audio] );
723                 }
724             }
725         }
726
727         if( p_main->b_video )
728         {
729             /* for spu, default is none */
730             i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
731             if( i_spu < 0 || i_spu > i_spu_nb )
732             {
733                 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
734                 i_spu = 0;
735             }
736             if( i_spu > 0 && i_spu_nb > 0 )
737             {
738                 i_spu += vts.manager_inf.i_audio_nb;
739                 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
740             }
741         }
742     } /* i_title >= 0 */
743     else
744     {
745         p_area = p_input->stream.p_selected_area;
746     }
747 #undef vts
748 #undef vmg
749
750     /*
751      * Chapter selection
752      */
753
754     if( p_area->i_part != p_dvd->i_chapter )
755     {
756         if( ( p_area->i_part > 0 ) &&
757             ( p_area->i_part <= p_area->i_part_nb ))
758         {
759             if( DVDChapterSelect( p_dvd, p_area->i_part ) < 0 )
760             {
761                 intf_ErrMsg( "dvd error: can't set chapter in area" );
762                 p_input->b_error = 1;
763                 return -1;
764             }
765     
766             p_input->stream.p_selected_area->i_tell =
767                                    LB2OFF( p_dvd->i_start ) - p_area->i_start;
768             p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
769     
770             intf_WarnMsg( 4, "dvd info: chapter %d start at: %lld",
771                                         p_area->i_part, p_area->i_tell );
772         }
773         else
774         {
775             p_area->i_part = 1;
776             p_dvd->i_chapter = 1;
777         }
778     }
779
780 #define title \
781     p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
782     if( p_area->i_angle != p_dvd->i_angle )
783     {
784         if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
785         {
786             if( ( p_area->i_angle - p_dvd->i_angle ) < 0 )
787             {
788                 p_dvd->i_cell = 0;
789             }
790             p_dvd->i_prg_cell += ( p_area->i_angle - p_dvd->i_angle );
791             p_dvd->i_angle = p_area->i_angle;
792     
793             DVDFindSector( p_dvd );
794             p_dvd->i_cell += p_dvd->i_angle_cell;
795         }
796         else
797         {
798             p_dvd->i_angle = p_area->i_angle;
799         }
800
801         intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
802     }
803
804     /* warn interface that something has changed */
805     p_input->stream.b_seekable = 1;
806     p_input->stream.b_changed = 1;
807
808     return 0;
809 }
810
811
812 /*****************************************************************************
813  * DVDRead: reads data packets.
814  *****************************************************************************
815  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
816  * packets.
817  *****************************************************************************/
818 static int DVDRead( input_thread_t * p_input,
819                     data_packet_t ** pp_data )
820 {
821     thread_dvd_data_t *     p_dvd;
822     struct iovec            p_vec[DVD_BLOCK_READ_ONCE];
823     u8 *                    pi_cur;
824     int                     i_block_once;
825     int                     i_packet_size;
826     int                     i_iovec;
827     int                     i_packet;
828     int                     i_pos;
829     int                     i_read_blocks;
830     int                     i_sector;
831     boolean_t               b_eoc;
832     data_packet_t *         p_data;
833
834     p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
835
836     *pp_data = NULL;
837
838     b_eoc = 0;
839     i_sector = p_dvd->i_title_start + p_dvd->i_sector;
840     i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
841
842
843     /* Get the position of the next cell if we're at cell end */
844     if( i_block_once <= 0 )
845     {
846         int     i_angle;
847
848         p_dvd->i_cell++;
849         p_dvd->i_angle_cell++;
850
851         /* Find cell index in adress map */
852         if( DVDFindSector( p_dvd ) < 0 )
853         {
854             intf_ErrMsg( "dvd error: can't find next cell" );
855             return 1;
856         }
857
858         /* Position the fd pointer on the right address */
859         if( ( i_sector = dvdcss_seek( p_dvd->dvdhandle,
860                                       p_dvd->i_title_start + p_dvd->i_sector,
861                                       DVDCSS_SEEK_MPEG ) ) < 0 )
862         {
863             intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
864             return -1;
865         }
866
867         /* update chapter : it will be easier when we have navigation
868          * ES support */
869         if( p_dvd->i_chapter < ( p_dvd->i_chapter_nb - 1 ) )
870         {
871             if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
872             {
873                 i_angle = p_dvd->i_angle - 1;
874             }
875             else
876             {
877                 i_angle = 0;
878             }
879             if( title.chapter_map.pi_start_cell[p_dvd->i_chapter] <=
880                 ( p_dvd->i_prg_cell - i_angle + 1 ) )
881             {
882                 p_dvd->i_chapter++;
883                 b_eoc = 1;
884             }
885         }
886
887         i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
888     }
889
890     /* The number of blocks read is the max between the requested
891      * value and the leaving block in the cell */
892     if( i_block_once > p_dvd->i_block_once )
893     {
894         i_block_once = p_dvd->i_block_once;
895     }
896 /*
897 intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
898 */
899
900     /* Get iovecs */
901     *pp_data = p_data = input_BuffersToIO( p_input->p_method_data, p_vec,
902                                            DVD_BLOCK_READ_ONCE );
903
904     if ( p_data == NULL )
905     {
906         return( -1 );
907     }
908
909     /* Reads from DVD */
910     i_read_blocks = dvdcss_readv( p_dvd->dvdhandle, p_vec,
911                                   i_block_once, DVDCSS_READ_DECRYPT );
912
913     /* Update global position */
914     p_dvd->i_sector += i_read_blocks;
915
916     i_packet = 0;
917
918     /* Read headers to compute payload length */
919     for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
920     {
921         data_packet_t * p_current = p_data;
922         i_pos = 0;
923
924         while( i_pos < DVD_LB_SIZE )
925         {
926             pi_cur = (u8*)p_vec[i_iovec].iov_base + i_pos;
927
928             /* Default header */
929             if( U32_AT( pi_cur ) != 0x1BA )
930             {
931                 /* That's the case for all packets, except pack header. */
932                 i_packet_size = U16_AT( pi_cur + 4 );
933             }
934             else
935             {
936                 /* MPEG-2 Pack header. */
937                 i_packet_size = 8;
938             }
939
940             if( i_pos != 0 )
941             {
942                 *pp_data = input_ShareBuffer( p_input->p_method_data,
943                                               p_current );
944             }
945             else
946             {
947                 *pp_data = p_data;
948                 p_data = p_data->p_next;
949             }
950
951             (*pp_data)->p_payload_start = (*pp_data)->p_demux_start =
952                     (*pp_data)->p_demux_start + i_pos;
953
954             (*pp_data)->p_payload_end =
955                     (*pp_data)->p_payload_start + i_packet_size + 6;
956
957             i_packet++;
958             i_pos += i_packet_size + 6;
959             pp_data = &(*pp_data)->p_next;
960         }
961     }
962
963     p_input->pf_delete_packet( p_input->p_method_data, p_data );
964     *pp_data = NULL;
965
966     vlc_mutex_lock( &p_input->stream.stream_lock );
967
968     p_input->stream.p_selected_area->i_tell =
969         LB2OFF( i_sector + i_read_blocks ) -
970         p_input->stream.p_selected_area->i_start;
971     if( b_eoc )
972     {
973         /* We modify i_part only at end of chapter not to erase
974          * some modification from the interface */
975         p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
976     }
977
978     if( p_input->stream.p_selected_area->i_tell
979             >= p_input->stream.p_selected_area->i_size )
980     {
981         if( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb )
982         {
983             /* EOF */
984             vlc_mutex_unlock( &p_input->stream.stream_lock );
985             return 0;
986         }
987
988         /* EOT */
989         intf_WarnMsg( 4, "dvd info: new title" );
990         p_dvd->i_title++;
991         DVDSetArea( p_input, p_input->stream.pp_areas[p_dvd->i_title] );
992         vlc_mutex_unlock( &p_input->stream.stream_lock );
993         return( i_packet );
994     }
995
996     vlc_mutex_unlock( &p_input->stream.stream_lock );
997
998     if( i_read_blocks != i_block_once )
999     {
1000         return -1;
1001     }
1002
1003     return( i_packet );
1004 }
1005
1006 /*****************************************************************************
1007  * DVDRewind : reads a stream backward
1008  *****************************************************************************/
1009 static int DVDRewind( input_thread_t * p_input )
1010 {
1011     return( -1 );
1012 }
1013
1014 /*****************************************************************************
1015  * DVDSeek : Goes to a given position on the stream.
1016  *****************************************************************************
1017  * This one is used by the input and translate chronological position from
1018  * input to logical position on the device.
1019  * The lock should be taken before calling this function.
1020  *****************************************************************************/
1021 static void DVDSeek( input_thread_t * p_input, off_t i_off )
1022 {
1023     thread_dvd_data_t *     p_dvd;
1024     int                     i_block;
1025     int                     i_prg_cell;
1026     int                     i_cell;
1027     int                     i_chapter;
1028     int                     i_angle;
1029     
1030     p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1031
1032     /* we have to take care of offset of beginning of title */
1033     p_dvd->i_sector = OFF2LB(i_off + p_input->stream.p_selected_area->i_start)
1034                        - p_dvd->i_title_start;
1035
1036     i_prg_cell = 0;
1037     i_chapter = 0;
1038
1039     /* parse vobu address map to find program cell */
1040     while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector  )
1041     {
1042         i_prg_cell++;
1043     }
1044
1045     p_dvd->i_prg_cell = i_prg_cell;
1046
1047     if( DVDChooseAngle( p_dvd ) < 0 )
1048     {
1049         p_input->b_error = 1;
1050         return;        
1051     }
1052
1053     p_dvd->i_cell = 0;
1054
1055     /* Find first title cell which is inside program cell */
1056     if( DVDFindCell( p_dvd ) < 0 )
1057     {
1058         /* no following cell : we're at eof */
1059         intf_ErrMsg( "dvd error: cell seeking failed" );
1060         p_input->b_error = 1;
1061         return;
1062     }
1063
1064     i_cell = p_dvd->i_cell;
1065
1066 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1067     /* parse cell address map to find title cell containing sector */
1068     while( cell.i_end_sector < p_dvd->i_sector )
1069     {
1070         i_cell++;
1071     }
1072
1073     p_dvd->i_cell = i_cell;
1074
1075     /* if we're inside a multi-angle zone, we have to choose i_sector
1076      * in the current angle ; we can't do it all the time since cells
1077      * can be very wide out of such zones */
1078     if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1079     {
1080         p_dvd->i_sector = MAX(
1081                 cell.i_start_sector,
1082                 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1083     }
1084
1085     p_dvd->i_end_sector = MIN(
1086             cell.i_end_sector,
1087             title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1088 #undef cell
1089     /* update chapter */
1090     if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1091     {
1092         i_angle = p_dvd->i_angle - 1;
1093     }
1094     else
1095     {
1096         i_angle = 0;
1097     }
1098     if( p_dvd->i_chapter_nb > 1 )
1099     {
1100         while( ( title.chapter_map.pi_start_cell[i_chapter] <=
1101                     ( p_dvd->i_prg_cell - i_angle + 1 ) ) &&
1102                ( i_chapter < ( p_dvd->i_chapter_nb - 1 ) ) )
1103         {
1104             i_chapter++;
1105         }
1106     }
1107     else
1108     {
1109         i_chapter = 1;
1110     }
1111
1112     p_dvd->i_chapter = i_chapter;
1113     p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1114
1115     if( ( i_block = dvdcss_seek( p_dvd->dvdhandle,
1116                                  p_dvd->i_title_start + p_dvd->i_sector,
1117                                  DVDCSS_SEEK_MPEG ) ) < 0 )
1118     {
1119         intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
1120         p_input->b_error = 1;
1121         return;
1122     }
1123
1124     p_input->stream.p_selected_area->i_tell =
1125         LB2OFF ( i_block ) - p_input->stream.p_selected_area->i_start;
1126
1127     intf_WarnMsg( 7, "Program Cell: %d Cell: %d Chapter: %d",
1128                      p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
1129
1130
1131     return;
1132 }
1133
1134 #define cell  p_dvd->p_ifo->vts.cell_inf
1135
1136 /*****************************************************************************
1137  * DVDFindCell: adjust the title cell index with the program cell
1138  *****************************************************************************/
1139 static int DVDFindCell( thread_dvd_data_t * p_dvd )
1140 {
1141     int                 i_cell;
1142     int                 i_index;
1143
1144     i_cell = p_dvd->i_cell;
1145     i_index = p_dvd->i_prg_cell;
1146
1147     if( i_cell >= cell.i_cell_nb )
1148     {
1149         return -1;
1150     }
1151
1152     while( ( ( title.p_cell_pos[i_index].i_vob_id !=
1153                    cell.p_cell_map[i_cell].i_vob_id ) ||
1154       ( title.p_cell_pos[i_index].i_cell_id !=
1155                    cell.p_cell_map[i_cell].i_cell_id ) ) &&
1156            ( i_cell < cell.i_cell_nb - 1 ) )
1157     {
1158         i_cell++;
1159     }
1160
1161 /*
1162 intf_WarnMsg( 12, "FindCell: i_cell %d i_index %d found %d nb %d",
1163                     p_dvd->i_cell,
1164                     p_dvd->i_prg_cell,
1165                     i_cell,
1166                     cell.i_cell_nb );
1167 */
1168
1169     p_dvd->i_cell = i_cell;
1170
1171     return 0;    
1172 }
1173
1174 #undef cell
1175
1176 /*****************************************************************************
1177  * DVDFindSector: find cell index in adress map from index in
1178  * information table program map and give corresponding sectors.
1179  *****************************************************************************/
1180 static int DVDFindSector( thread_dvd_data_t * p_dvd )
1181 {
1182
1183     if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
1184     {
1185         p_dvd->i_prg_cell++;
1186
1187         if( DVDChooseAngle( p_dvd ) < 0 )
1188         {
1189             return -1;
1190         }
1191     }
1192
1193     if( DVDFindCell( p_dvd ) < 0 )
1194     {
1195         intf_ErrMsg( "dvd error: can't find sector" );
1196         return -1;
1197     }
1198     
1199     /* Find start and end sectors of new cell */
1200 #if 1
1201     p_dvd->i_sector = MAX(
1202          p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1203          title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1204     p_dvd->i_end_sector = MIN(
1205          p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1206          title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1207 #else
1208     p_dvd->i_sector = title.p_cell_play[p_dvd->i_prg_cell].i_start_sector;
1209     p_dvd->i_end_sector = title.p_cell_play[p_dvd->i_prg_cell].i_end_sector;
1210 #endif
1211
1212 /*
1213     intf_WarnMsg( 12, "cell: %d sector1: 0x%x end1: 0x%x\n"
1214                    "index: %d sector2: 0x%x end2: 0x%x\n"
1215                    "category: 0x%x ilvu end: 0x%x vobu start 0x%x", 
1216         p_dvd->i_cell,
1217         p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1218         p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1219         p_dvd->i_prg_cell,
1220         title.p_cell_play[p_dvd->i_prg_cell].i_start_sector,
1221         title.p_cell_play[p_dvd->i_prg_cell].i_end_sector,
1222         title.p_cell_play[p_dvd->i_prg_cell].i_category, 
1223         title.p_cell_play[p_dvd->i_prg_cell].i_first_ilvu_vobu_esector,
1224         title.p_cell_play[p_dvd->i_prg_cell].i_last_vobu_start_sector );
1225 */
1226
1227     return 0;
1228 }
1229
1230 /*****************************************************************************
1231  * DVDChapterSelect: find the cell corresponding to requested chapter
1232  *****************************************************************************/
1233 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
1234 {
1235
1236     /* Find cell index in Program chain for current chapter */
1237     p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
1238     p_dvd->i_cell = 0;
1239     p_dvd->i_sector = 0;
1240
1241     DVDChooseAngle( p_dvd );
1242
1243     /* Search for cell_index in cell adress_table and initialize
1244      * start sector */
1245     if( DVDFindSector( p_dvd ) < 0 )
1246     {
1247         intf_ErrMsg( "dvd error: can't select chapter" );
1248         return -1;
1249     }
1250
1251     /* start is : beginning of vts vobs + offset to vob x */
1252     p_dvd->i_start = p_dvd->i_title_start + p_dvd->i_sector;
1253
1254     /* Position the fd pointer on the right address */
1255     if( ( p_dvd->i_start = dvdcss_seek( p_dvd->dvdhandle,
1256                                         p_dvd->i_start,
1257                                         DVDCSS_SEEK_MPEG ) ) < 0 )
1258     {
1259         intf_ErrMsg( "dvd error: %s", dvdcss_error( p_dvd->dvdhandle ) );
1260         return -1;
1261     }
1262
1263     p_dvd->i_chapter = i_chapter;
1264     return 0;
1265 }
1266
1267 /*****************************************************************************
1268  * DVDChooseAngle: select the cell corresponding to the selected angle
1269  *****************************************************************************/
1270 static int DVDChooseAngle( thread_dvd_data_t * p_dvd )
1271 {
1272     /* basic handling of angles */
1273     switch( ( ( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1274                     >> 12 ) )
1275     {
1276         /* we enter a muli-angle section */
1277         case 0x5:
1278             p_dvd->i_prg_cell += p_dvd->i_angle - 1;
1279             p_dvd->i_angle_cell = 0;
1280             break;
1281         /* we exit a multi-angle section */
1282         case 0x9:
1283         case 0xd:
1284             p_dvd->i_prg_cell += p_dvd->i_angle_nb - p_dvd->i_angle;
1285             break;
1286     }
1287
1288     return 0;
1289 }
1290
1291 #undef title