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