]> git.sesse.net Git - vlc/blob - plugins/dvd/input_dvd.c
First serie of changes in DVD module for the forthcoming interface menus
[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  *  -input_netlist used to read packets
8  *  -dvd_ifo for ifo parsing and analyse
9  *  -dvd_css for unscrambling
10  *  -dvd_udf to find files
11  *****************************************************************************
12  * Copyright (C) 1998-2001 VideoLAN
13  * $Id: input_dvd.c,v 1.15 2001/02/20 02:53:13 stef Exp $
14  *
15  * Author: Stéphane Borel <stef@via.ecp.fr>
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 2 of the License, or
20  * (at your option) any later version.
21  * 
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
30  *****************************************************************************/
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35 #include "defs.h"
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <netinet/in.h>
41
42 #include <fcntl.h>
43 #include <sys/types.h>
44 #include <sys/uio.h>
45
46 #include <string.h>
47 #include <errno.h>
48 #include <malloc.h>
49
50 #include "config.h"
51 #include "common.h"
52 #include "threads.h"
53 #include "mtime.h"
54 #include "tests.h"
55
56 #include "intf_msg.h"
57
58 #include "main.h"
59
60 #include "stream_control.h"
61 #include "input_ext-intf.h"
62 #include "input_ext-dec.h"
63
64 #include "input.h"
65 #include "input_netlist.h"
66
67 #include "dvd_ifo.h"
68 #include "dvd_css.h"
69 #include "input_dvd.h"
70 #include "mpeg_system.h"
71
72 #include "debug.h"
73
74 #include "modules.h"
75
76 /*****************************************************************************
77  * Local tables
78  *****************************************************************************/
79 static struct
80 {
81         char    p_code[3];
82     char    p_lang_long[20];
83 } lang_tbl[] =
84     {
85
86     /* The ISO 639 language codes.
87      * Language names with * prefix are not spelled in their own language 
88      */
89         {"  ", "Not Specified"},
90         {"aa", "*Afar"},
91         {"ab", "*Abkhazian"},
92         {"af", "*Afrikaans"},
93         {"am", "*Amharic"},
94         {"ar", "*Arabic"},
95         {"as", "*Assamese"},
96         {"ay", "*Aymara"},
97         {"az", "*Azerbaijani"},
98         {"ba", "*Bashkir"},
99         {"be", "*Byelorussian"},
100         {"bg", "*Bulgarian"},
101         {"bh", "*Bihari"},
102         {"bi", "*Bislama"},
103         {"bn", "*Bengali; Bangla"},
104         {"bo", "*Tibetan"},
105         {"br", "*Breton"},
106         {"ca", "*Catalan"},
107         {"co", "*Corsican"},
108         {"cs", "*Czech(Ceske)"},
109         {"cy", "*Welsh"},
110         {"da", "Dansk"},
111         {"de", "Deutsch"},
112         {"dz", "*Bhutani"},
113         {"el", "*Greek"},
114         {"en", "English"},
115         {"eo", "*Esperanto"},
116         {"es", "Espanol"},
117         {"et", "*Estonian"},
118         {"eu", "*Basque"},
119         {"fa", "*Persian"},
120         {"fi", "Suomi"},
121         {"fj", "*Fiji"},
122         {"fo", "*Faroese"},
123         {"fr", "Francais"},
124         {"fy", "*Frisian"},
125         {"ga", "*Irish"},
126         {"gd", "*Scots Gaelic"},
127         {"gl", "*Galician"},
128         {"gn", "*Guarani"},
129         {"gu", "*Gujarati"},
130         {"ha", "*Hausa"},
131         {"he", "*Hebrew"},                              // formerly iw
132         {"hi", "*Hindi"},
133         {"hr", "Hrvatski"},                             // Croatian
134         {"hu", "Magyar"},
135         {"hy", "*Armenian"},
136         {"ia", "*Interlingua"},
137         {"id", "*Indonesian"},                          // formerly in
138         {"ie", "*Interlingue"},
139         {"ik", "*Inupiak"},
140         {"in", "*Indonesian"},                          // replaced by id
141         {"is", "Islenska"},
142         {"it", "Italiano"},
143         {"iu", "*Inuktitut"},
144         {"iw", "*Hebrew"},                              // replaced by he
145         {"ja", "*Japanese"},
146         {"ji", "*Yiddish"},                             // replaced by yi
147         {"jw", "*Javanese"},
148         {"ka", "*Georgian"},
149         {"kk", "*Kazakh"},
150         {"kl", "*Greenlandic"},
151         {"km", "*Cambodian"},
152         {"kn", "*Kannada"},
153         {"ko", "*Korean"},
154         {"ks", "*Kashmiri"},
155         {"ku", "*Kurdish"},
156         {"ky", "*Kirghiz"},
157         {"la", "*Latin"},
158         {"ln", "*Lingala"},
159         {"lo", "*Laothian"},
160         {"lt", "*Lithuanian"},
161         {"lv", "*Latvian, Lettish"},
162         {"mg", "*Malagasy"},
163         {"mi", "*Maori"},
164         {"mk", "*Macedonian"},
165         {"ml", "*Malayalam"},
166         {"mn", "*Mongolian"},
167         {"mo", "*Moldavian"},
168         {"mr", "*Marathi"},
169         {"ms", "*Malay"},
170         {"mt", "*Maltese"},
171         {"my", "*Burmese"},
172         {"na", "*Nauru"},
173         {"ne", "*Nepali"},
174         {"nl", "Nederlands"},
175         {"no", "Norsk"},
176         {"oc", "*Occitan"},
177         {"om", "*(Afan) Oromo"},
178         {"or", "*Oriya"},
179         {"pa", "*Punjabi"},
180         {"pl", "*Polish"},
181         {"ps", "*Pashto, Pushto"},
182         {"pt", "Portugues"},
183         {"qu", "*Quechua"},
184         {"rm", "*Rhaeto-Romance"},
185         {"rn", "*Kirundi"},
186         {"ro", "*Romanian"},
187         {"ru", "*Russian"},
188         {"rw", "*Kinyarwanda"},
189         {"sa", "*Sanskrit"},
190         {"sd", "*Sindhi"},
191         {"sg", "*Sangho"},
192         {"sh", "*Serbo-Croatian"},
193         {"si", "*Sinhalese"},
194         {"sk", "*Slovak"},
195         {"sl", "*Slovenian"},
196         {"sm", "*Samoan"},
197         {"sn", "*Shona"},
198         {"so", "*Somali"},
199         {"sq", "*Albanian"},
200         {"sr", "*Serbian"},
201         {"ss", "*Siswati"},
202         {"st", "*Sesotho"},
203         {"su", "*Sundanese"},
204         {"sv", "Svenska"},
205         {"sw", "*Swahili"},
206         {"ta", "*Tamil"},
207         {"te", "*Telugu"},
208         {"tg", "*Tajik"},
209         {"th", "*Thai"},
210         {"ti", "*Tigrinya"},
211         {"tk", "*Turkmen"},
212         {"tl", "*Tagalog"},
213         {"tn", "*Setswana"},
214         {"to", "*Tonga"},
215         {"tr", "*Turkish"},
216         {"ts", "*Tsonga"},
217         {"tt", "*Tatar"},
218         {"tw", "*Twi"},
219         {"ug", "*Uighur"},
220         {"uk", "*Ukrainian"},
221         {"ur", "*Urdu"},
222         {"uz", "*Uzbek"},
223         {"vi", "*Vietnamese"},
224         {"vo", "*Volapuk"},
225         {"wo", "*Wolof"},
226         {"xh", "*Xhosa"},
227         {"yi", "*Yiddish"},                             // formerly ji
228         {"yo", "*Yoruba"},
229         {"za", "*Zhuang"},
230         {"zh", "*Chinese"},
231         {"zu", "*Zulu"},
232         {"\0", ""}
233     };
234
235 /*****************************************************************************
236  * Local prototypes
237  *****************************************************************************/
238 static int  DVDProbe    ( probedata_t *p_data );
239 static int  DVDCheckCSS ( struct input_thread_s * );
240 static int  DVDRead     ( struct input_thread_s *, data_packet_t ** );
241 static void DVDInit     ( struct input_thread_s * );
242 static void DVDEnd      ( struct input_thread_s * );
243 static void DVDSeek     ( struct input_thread_s *, off_t );
244 static int  DVDSetArea  ( struct input_thread_s *, int, int, int, int );
245 static int  DVDRewind   ( struct input_thread_s * );
246
247 /*****************************************************************************
248  * Functions exported as capabilities. They are declared as static so that
249  * we don't pollute the namespace too much.
250  *****************************************************************************/
251 void input_getfunctions( function_list_t * p_function_list )
252 {
253 #define input p_function_list->functions.input
254     p_function_list->pf_probe = DVDProbe;
255     input.pf_init             = DVDInit;
256     input.pf_open             = input_FileOpen;
257     input.pf_close            = input_FileClose;
258     input.pf_end              = DVDEnd;
259     input.pf_read             = DVDRead;
260     input.pf_set_area         = DVDSetArea;
261     input.pf_demux            = input_DemuxPS;
262     input.pf_new_packet       = input_NetlistNewPacket;
263     input.pf_new_pes          = input_NetlistNewPES;
264     input.pf_delete_packet    = input_NetlistDeletePacket;
265     input.pf_delete_pes       = input_NetlistDeletePES;
266     input.pf_rewind           = DVDRewind;
267     input.pf_seek             = DVDSeek;
268 #undef input
269 }
270
271 /*
272  * Local tools to decode some data in ifo
273  */
274
275 /*****************************************************************************
276  * Language: gives the long language name from the two-letters ISO-639 code
277  *****************************************************************************/
278 static char * Language( char * p_code )
279 {
280     int     i = 0;
281
282     while( !memcmp( lang_tbl[i].p_code, p_code, 2 ) && lang_tbl[i].p_lang_long )
283     {
284         i++;
285     }
286
287     return lang_tbl[i].p_lang_long;
288 }
289
290 /*
291  * Data reading functions
292  */
293
294 /*****************************************************************************
295  * DVDProbe: verifies that the stream is a PS stream
296  *****************************************************************************/
297 static int DVDProbe( probedata_t *p_data )
298 {
299     input_thread_t * p_input = (input_thread_t *)p_data;
300
301     char * psz_name = p_input->p_source;
302     int i_handle;
303     int i_score = 5;
304
305     if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
306     {
307         return( 999 );
308     }
309
310     if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
311     {
312         /* If the user specified "dvd:" then it's probably a DVD */
313         i_score = 100;
314         psz_name += 4;
315     }
316
317     i_handle = open( psz_name, 0 );
318     if( i_handle == -1 )
319     {
320         return( 0 );
321     }
322     close( i_handle );
323
324     return( i_score );
325 }
326
327 /*****************************************************************************
328  * DVDCheckCSS: check the stream
329  *****************************************************************************/
330 static int DVDCheckCSS( input_thread_t * p_input )
331 {
332 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
333     return CSSTest( p_input->i_handle );
334 #else
335     /* DVD ioctls unavailable.
336      * FIXME: Check the stream to see whether it is encrypted or not 
337      * to give and accurate error message */
338     return 0;
339 #endif
340 }
341
342 /*****************************************************************************
343  * DVDSetArea: initialize input data for title x, chapter y.
344  * It should be called for each user navigation request, and to change
345  * audio or sub-picture streams.
346  * ---
347  * Take care that i_title and i_chapter start from 0.
348  *****************************************************************************/
349 static int DVDSetArea( input_thread_t * p_input,
350                        int i_title, int i_chapter,
351                        int i_audio, int i_spu )
352 {
353     thread_dvd_data_t *  p_method;
354     es_descriptor_t *    p_es;
355     off_t                i_start;
356     off_t                i_size;
357     pgc_t *              p_pgc;
358     int                  i_start_cell;
359     int                  i_end_cell;
360     int                  i_index;
361     int                  i_cell;
362     int                  i_nb;
363     int                  i_id;
364     int                  i;
365     
366     p_method = (thread_dvd_data_t*)p_input->p_plugin_data;
367
368     /* Ifo structures reading */
369     p_method->ifo.i_title = i_title;
370     IfoReadVTS( &(p_method->ifo) );
371     intf_WarnMsg( 2, "Ifo: VTS initialized" );
372
373 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
374     if( p_method->b_encrypted )
375     {
376         p_method->css.i_title = i_title;
377         p_method->css.i_title_pos =
378                 p_method->ifo.vts.i_pos +
379                 p_method->ifo.vts.mat.i_tt_vobs_ssector * DVD_LB_SIZE;
380         CSSGetKey( &(p_method->css) );
381         intf_WarnMsg( 2, "CSS: VTS key initialized" );
382     }
383 #endif
384
385     /* Set selected title start and size */
386     p_pgc = &p_method->ifo.vts.pgci_ti.p_srp[0].pgc;
387
388     /* Find cell index in Program chain */
389     i_index = p_pgc->prg_map.pi_entry_cell[i_chapter] - 1;
390
391     /* Search for cell_index in cell adress_table */
392     i_cell = 0;
393     while( p_pgc->p_cell_pos_inf[i_index].i_vob_id >
394            p_method->ifo.vts.c_adt.p_cell_inf[i_cell].i_vob_id )
395     {
396         i_cell++;
397     }
398     while( p_pgc->p_cell_pos_inf[i_index].i_cell_id >
399            p_method->ifo.vts.c_adt.p_cell_inf[i_cell].i_cell_id )
400     {
401         i_cell++;
402     }
403     i_start_cell = i_cell;
404     i_end_cell = i_start_cell + p_pgc->i_cell_nb - 1;
405
406     intf_WarnMsg( 3, "DVD: Start cell: %d End Cell: %d",
407                                             i_start_cell, i_end_cell );
408
409     p_method->i_start_cell = i_start_cell;
410     p_method->i_end_cell = i_end_cell;
411
412     /* start is : beginning of vts + offset to vobs + offset to vob x */
413     i_start = p_method->ifo.vts.i_pos + DVD_LB_SIZE *
414             ( p_method->ifo.vts.mat.i_tt_vobs_ssector +
415               p_method->ifo.vts.c_adt.p_cell_inf[i_start_cell].i_ssector );
416
417     i_start = lseek( p_input->i_handle, i_start, SEEK_SET );
418     intf_WarnMsg( 2, "DVD: VOBstart at: %lld", i_start );
419
420     i_size = (off_t)
421         ( p_method->ifo.vts.c_adt.p_cell_inf[i_end_cell].i_esector -
422           p_method->ifo.vts.c_adt.p_cell_inf[i_start_cell].i_ssector + 1 )
423         *DVD_LB_SIZE;
424     intf_WarnMsg( 2, "DVD: stream size: %lld", i_size );
425
426 #if 0
427     p_es = NULL;
428
429     vlc_mutex_lock( &p_input->stream.stream_lock );
430
431     /* ES 0 -> video MPEG2 */
432     intf_WarnMsg( 1, "DVD: Video MPEG2 stream" );
433     p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
434     p_es->i_stream_id = 0xe0;
435     p_es->i_type = MPEG2_VIDEO_ES;
436     input_SelectES( p_input, p_es );
437
438     /* Audio ES, in the order they appear in .ifo */
439     i_nb = p_method->ifo.vts.mat.i_audio_nb;
440     intf_WarnMsg( 1, "DVD: Audio streams %d", i_nb );
441     for( i = 0 ; i < i_nb ; i++ )
442     {
443         i_id = ( ( 0x80 + i ) << 8 ) | 0xbd;
444         p_es = input_AddES( p_input,
445                            p_input->stream.pp_programs[0], i_id, 0 );
446         p_es->i_stream_id = 0xbd;
447         p_es->i_type = AC3_AUDIO_ES;
448         p_es->b_audio = 1;
449 //        p_es->psz_desc = p_method->ifo.vts.mat.pi_audio_attr[i];
450         if( i == 0 )
451         {
452             input_SelectES( p_input, p_es );
453         }
454     }
455
456     /* Sub Picture ES */
457     i_nb = p_method->ifo.vts.mat.i_subpic_nb;
458     intf_WarnMsg( 1, "DVD: Subpic streams %d", i_nb );
459     for( i = 0 ; i < i_nb ; i++ )
460     {
461         i_id = ( ( 0x20 + i ) << 8 ) | 0xbd;
462         p_es = input_AddES( p_input,
463                            p_input->stream.pp_programs[0], i_id, 0 );
464         p_es->i_stream_id = 0xbd;
465         p_es->i_type = DVD_SPU_ES;
466 //        p_es->psz_desc = p_method->ifo.vts.mat.pi_subpic_attr[i];
467         if( i == 0 )
468         {
469             input_SelectES( p_input, p_es );
470         }
471
472     }
473
474     /* area definition */
475     p_input->stream.pp_areas[0]->i_start = i_start;
476     p_input->stream.pp_areas[0]->i_size = i_size;
477
478     /* No PSM to read in DVD mode */
479     p_input->stream.pp_programs[0]->b_is_ok = 1;
480
481     vlc_mutex_unlock( &p_input->stream.stream_lock );
482
483 #else
484
485     if( p_input->stream.b_seekable )
486     {
487         stream_ps_data_t * p_demux_data =
488              (stream_ps_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
489
490         /* Pre-parse the stream to gather stream_descriptor_t. */
491         p_input->stream.pp_programs[0]->b_is_ok = 0;
492         p_demux_data->i_PSM_version = EMPTY_PSM_VERSION;
493
494         while( !p_input->b_die && !p_input->b_error
495                 && !p_demux_data->b_has_PSM )
496         {
497             int                 i_result, i;
498             data_packet_t *     pp_packets[INPUT_READ_ONCE];
499
500             i_result = DVDRead( p_input, pp_packets );
501             if( i_result == 1 )
502             {
503                 /* EOF */
504                 vlc_mutex_lock( &p_input->stream.stream_lock );
505                 p_input->stream.pp_programs[0]->b_is_ok = 1;
506                 vlc_mutex_unlock( &p_input->stream.stream_lock );
507                 break;
508             }
509             if( i_result == -1 )
510             {
511                 p_input->b_error = 1;
512                 break;
513             }
514
515             for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
516             {
517                 /* FIXME: use i_p_config_t */
518                 input_ParsePS( p_input, pp_packets[i] );
519                 input_NetlistDeletePacket( p_input->p_method_data,
520                                            pp_packets[i] );
521             }
522
523             /* File too big. */
524             if( p_input->stream.pp_areas[0]->i_tell > INPUT_PREPARSE_LENGTH )
525             {
526                 break;
527             }
528         }
529         lseek( p_input->i_handle, i_start, SEEK_SET );
530         vlc_mutex_lock( &p_input->stream.stream_lock );
531
532         /* i_tell is an indicator from the beginning of the stream,
533          * not of the DVD */
534         p_input->stream.pp_areas[0]->i_tell = 0;
535
536         if( p_demux_data->b_has_PSM )
537         {
538             /* (The PSM decoder will care about spawning the decoders) */
539             p_input->stream.pp_programs[0]->b_is_ok = 1;
540         }
541 #ifdef AUTO_SPAWN
542         else
543         {
544             /* (We have to do it ourselves) */
545             int                 i_es;
546
547             /* FIXME: we should do multiple passes in case an audio type
548              * is not present */
549             for( i_es = 0;
550                  i_es < p_input->stream.pp_programs[0]->i_es_number;
551                  i_es++ )
552             {
553 #define p_es p_input->stream.pp_programs[0]->pp_es[i_es]
554                 switch( p_es->i_type )
555                 {
556                     case MPEG1_VIDEO_ES:
557                     case MPEG2_VIDEO_ES:
558                         input_SelectES( p_input, p_es );
559                         break;
560
561                     case MPEG1_AUDIO_ES:
562                     case MPEG2_AUDIO_ES:
563                         if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
564                                 == (p_es->i_id & 0x1F) )
565                         switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
566                         {
567                         case 0:
568                             main_PutIntVariable( INPUT_AUDIO_VAR,
569                                                  REQUESTED_MPEG );
570                         case REQUESTED_MPEG:
571                             input_SelectES( p_input, p_es );
572                         }
573                         break;
574
575                     case AC3_AUDIO_ES:
576                         if( main_GetIntVariable( INPUT_CHANNEL_VAR, 0 )
577                                 == ((p_es->i_id & 0xF00) >> 8) )
578                         switch( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) )
579                         {
580                         case 0:
581                             main_PutIntVariable( INPUT_AUDIO_VAR,
582                                                  REQUESTED_AC3 );
583                         case REQUESTED_AC3:
584                             input_SelectES( p_input, p_es );
585                         }
586                         break;
587
588                     case DVD_SPU_ES:
589                         if( main_GetIntVariable( INPUT_SUBTITLE_VAR, -1 )
590                                 == ((p_es->i_id & 0x1F00) >> 8) )
591                         {
592                             input_SelectES( p_input, p_es );
593                         }
594                         break;
595
596                     case LPCM_AUDIO_ES:
597                         /* FIXME ! */
598                         break;
599                 }
600             }
601                     
602         }
603 #endif
604 #ifdef STATS
605         input_DumpStream( p_input );
606 #endif
607
608         /* FIXME : ugly kludge */
609         p_input->stream.pp_areas[0]->i_size = i_size;
610
611         vlc_mutex_unlock( &p_input->stream.stream_lock );
612     }
613     else
614     {
615         /* The programs will be added when we read them. */
616         vlc_mutex_lock( &p_input->stream.stream_lock );
617         p_input->stream.pp_programs[0]->b_is_ok = 0;
618
619         /* FIXME : ugly kludge */
620         p_input->stream.pp_areas[0]->i_size = i_size;
621
622         vlc_mutex_unlock( &p_input->stream.stream_lock );
623     }
624 #endif  
625
626     return 0;
627 }
628
629 /*****************************************************************************
630  * DVDInit: initializes DVD structures
631  *****************************************************************************/
632 static void DVDInit( input_thread_t * p_input )
633 {
634     thread_dvd_data_t *  p_method;
635     int                  i;
636
637     if( (p_method = malloc( sizeof(thread_dvd_data_t) )) == NULL )
638     {
639         intf_ErrMsg( "Out of memory" );
640         p_input->b_error = 1;
641         return;
642     }
643
644     p_input->p_plugin_data = (void *)p_method;
645     p_input->p_method_data = NULL;
646
647     p_method->i_fd = p_input->i_handle;
648     /* FIXME: read several packets once */
649     p_method->i_read_once = 1; 
650     p_method->b_encrypted = DVDCheckCSS( p_input );
651
652     lseek( p_input->i_handle, 0, SEEK_SET );
653
654     /* Reading structures initialisation */
655     input_NetlistInit( p_input, 4096, 4096, DVD_LB_SIZE,
656                        p_method->i_read_once ); 
657     intf_WarnMsg( 2, "DVD: Netlist initialized" );
658
659     /* Ifo initialisation */
660     p_method->ifo = IfoInit( p_input->i_handle );
661     intf_WarnMsg( 2, "Ifo: VMG initialized" );
662
663     /* CSS initialisation */
664     if( p_method->b_encrypted )
665     {
666
667 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
668         p_method->css = CSSInit( p_input->i_handle );
669
670         if( ( p_input->b_error = p_method->css.b_error ) )
671         {
672             intf_ErrMsg( "CSS fatal error" );
673             return;
674         }
675         intf_WarnMsg( 2, "CSS: initialized" );
676 #else
677         intf_ErrMsg( "Unscrambling not supported" );
678         p_input->b_error = 1;
679         return;
680 #endif
681     }
682
683     /* Initialize ES structures */
684     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
685     input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
686
687     /* Set stream and area data */
688     vlc_mutex_lock( &p_input->stream.stream_lock );
689
690     /* FIXME: We consider here that one title is one title set
691      * it is not true !!! */
692
693 #define area p_input->stream.pp_areas
694     /* We start from 1 here since area 0 is reserved for video_ts.vob */
695     for( i = 1 ; i < p_method->ifo.vmg.mat.i_tts_nb ; i++ )
696     {
697         input_AddArea( p_input );
698
699         /* Should not be so simple eventually :
700          * are title Program Chains, or still something else ? */
701         area[i]->i_id = i;
702
703         /* Absolute start offset and size 
704          * We can only set that with vts ifo, so we do it during the
705          * first call to DVDSetArea */
706         area[i]->i_start = 0;
707         area[i]->i_size = 0;
708
709         /* Number of chapter */
710         area[i]->i_part_nb = 0;
711         area[i]->i_part = 1;
712         /* Offset to vts_i_0.ifo */
713         area[i]->i_plugin_data = p_method->ifo.i_off +
714             ( p_method->ifo.vmg.ptt_srpt.p_tts[i-1].i_ssector * DVD_LB_SIZE );
715     }   
716 #undef area
717
718     vlc_mutex_unlock( &p_input->stream.stream_lock );
719
720     /* By default, set all parameters to 0 */
721     /* FIXME: wrong kludge to get first title number */
722     DVDSetArea( p_input, p_method->ifo.vmg.ptt_srpt.p_tts[0].i_tts_nb - 1,
723                 0, 0, 0 );
724
725     return;
726 }
727
728 /*****************************************************************************
729  * DVDEnd: frees unused data
730  *****************************************************************************/
731 static void DVDEnd( input_thread_t * p_input )
732 {
733     /* FIXME: check order of calls */
734 //    CSSEnd( p_input );
735 //    IfoEnd( (ifo_t*)(&p_input->p_plugin_data->ifo ) );
736     free( p_input->stream.p_demux_data );
737     free( p_input->p_plugin_data );
738     input_NetlistEnd( p_input );
739 }
740
741 /*****************************************************************************
742  * DVDRead: reads data packets into the netlist.
743  *****************************************************************************
744  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
745  * EOF.
746  *****************************************************************************/
747 static int DVDRead( input_thread_t * p_input,
748                    data_packet_t ** pp_packets )
749 {
750     thread_dvd_data_t *     p_method;
751     netlist_t *             p_netlist;
752     struct iovec *          p_vec;
753     struct data_packet_s *  p_data;
754     u8 *                    pi_cur;
755     int                     i_packet_size;
756     int                     i_packet;
757     int                     i_pos;
758     int                     i;
759     boolean_t               b_first_packet;
760
761     p_method = ( thread_dvd_data_t * ) p_input->p_plugin_data;
762     p_netlist = ( netlist_t * ) p_input->p_method_data;
763
764     /* Get an iovec pointer */
765     if( ( p_vec = input_NetlistGetiovec( p_netlist ) ) == NULL )
766     {
767         intf_ErrMsg( "DVD: read error" );
768         return -1;
769     }
770
771     /* Reads from DVD */
772     readv( p_input->i_handle, p_vec, p_method->i_read_once );
773
774 #if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
775     if( p_method->b_encrypted )
776     {
777         for( i=0 ; i<p_method->i_read_once ; i++ )
778         {
779             CSSDescrambleSector( p_method->css.pi_title_key, 
780                                  p_vec[i].iov_base );
781             ((u8*)(p_vec[i].iov_base))[0x14] &= 0x8F;
782         }
783     }
784 #endif
785
786     /* Update netlist indexes */
787     input_NetlistMviovec( p_netlist, p_method->i_read_once, &p_data );
788
789     i_packet = 0;
790     /* Read headers to compute payload length */
791     for( i = 0 ; i < p_method->i_read_once ; i++ )
792     {
793         i_pos = 0;
794         b_first_packet = 1;
795         while( i_pos < p_netlist->i_buffer_size )
796         {
797             pi_cur = (u8*)(p_vec[i].iov_base + i_pos);
798             /*default header */
799             if( U32_AT( pi_cur ) != 0x1BA )
800             {
801                 /* That's the case for all packets, except pack header. */
802                 i_packet_size = U16_AT( pi_cur + 4 );
803             }
804             else
805             {
806                 /* Pack header. */
807                 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
808                 {
809                     /* MPEG-2 */
810                     i_packet_size = 8;
811                 }
812                 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
813                 {
814                     /* MPEG-1 */
815                     i_packet_size = 6;
816                 }
817                 else
818                 {
819                     intf_ErrMsg( "Unable to determine stream type" );
820                     return( -1 );
821                 }
822             }
823             if( b_first_packet )
824             {
825                 p_data->b_discard_payload = 0;
826                 b_first_packet = 0;
827             }
828             else
829             { 
830                 p_data = input_NetlistNewPacket( p_netlist ,
831                                                  i_packet_size + 6 );
832                 memcpy( p_data->p_buffer,
833                         p_vec[i].iov_base + i_pos , i_packet_size + 6 );
834             }
835
836             p_data->p_payload_end = p_data->p_payload_start + i_packet_size + 6;
837             pp_packets[i_packet] = p_data;
838             i_packet++;
839             i_pos += i_packet_size + 6;
840         }
841     }
842     pp_packets[i_packet] = NULL;
843
844     vlc_mutex_lock( &p_input->stream.stream_lock );
845     p_input->stream.pp_areas[0]->i_tell += p_method->i_read_once *DVD_LB_SIZE;
846     vlc_mutex_unlock( &p_input->stream.stream_lock );
847
848     return( 0 );
849 }
850
851
852 /*****************************************************************************
853  * DVDRewind : reads a stream backward
854  *****************************************************************************/
855 static int DVDRewind( input_thread_t * p_input )
856 {
857     return( -1 );
858 }
859
860 /*****************************************************************************
861  * DVDSeek : Goes to a given position on the stream ; this one is used by the 
862  * input and translate chronological position from input to logical postion
863  * on the device
864  *****************************************************************************/
865 static void DVDSeek( input_thread_t * p_input, off_t i_off )
866 {
867     thread_dvd_data_t *     p_method;
868     off_t                   i_pos;
869     
870     p_method = ( thread_dvd_data_t * )p_input->p_plugin_data;
871
872     /* We have to take care of offset of beginning of title */
873     i_pos = i_off + p_input->stream.pp_areas[0]->i_start;
874
875     /* With DVD, we have to be on a sector boundary */
876     i_pos = i_pos & (~0x7ff);
877
878     i_pos = lseek( p_input->i_handle, i_pos, SEEK_SET );
879
880     p_input->stream.pp_areas[0]->i_tell = i_pos - p_input->stream.pp_areas[0]->i_start;
881
882     return;
883 }