]> git.sesse.net Git - vlc/blob - plugins/dvd/input_dvd.c
-completely changed title and chapter initialization. Now we have real
[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.25 2001/03/03 07:07:01 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 #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
69 #include "dvd_netlist.h"
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 }
87
88 lang_tbl[] =
89 {
90     /* The ISO 639 language codes.
91      * Language names with * prefix are not spelled in their own language 
92      */
93     { "  ", "Not Specified" },
94     { "aa", "*Afar" },
95     { "ab", "*Abkhazian" },
96     { "af", "*Afrikaans" },
97     { "am", "*Amharic" },
98     { "ar", "*Arabic" },
99     { "as", "*Assamese" },
100     { "ay", "*Aymara" },
101     { "az", "*Azerbaijani" },
102     { "ba", "*Bashkir" },
103     { "be", "*Byelorussian" },
104     { "bg", "*Bulgarian" },
105     { "bh", "*Bihari" },
106     { "bi", "*Bislama" },
107     { "bn", "*Bengali; Bangla" },
108     { "bo", "*Tibetan" },
109     { "br", "*Breton" },
110     { "ca", "*Catalan" },
111     { "co", "*Corsican" },
112     { "cs", "*Czech(Ceske)" },
113     { "cy", "*Welsh" },
114     { "da", "Dansk" },
115     { "de", "Deutsch" },
116     { "dz", "*Bhutani" },
117     { "el", "*Greek" },
118     { "en", "English" },
119     { "eo", "*Esperanto" },
120     { "es", "Espanol" },
121     { "et", "*Estonian" },
122     { "eu", "*Basque" },
123     { "fa", "*Persian" },
124     { "fi", "Suomi" },
125     { "fj", "*Fiji" },
126     { "fo", "*Faroese" },
127     { "fr", "Francais" },
128     { "fy", "*Frisian" },
129     { "ga", "*Irish" },
130     { "gd", "*Scots Gaelic" },
131     { "gl", "*Galician" },
132     { "gn", "*Guarani" },
133     { "gu", "*Gujarati" },
134     { "ha", "*Hausa" },
135     { "he", "*Hebrew" },                                      /* formerly iw */
136     { "hi", "*Hindi" },
137     { "hr", "Hrvatski" },                                        /* Croatian */
138     { "hu", "Magyar" },
139     { "hy", "*Armenian" },
140     { "ia", "*Interlingua" },
141     { "id", "*Indonesian" },                                  /* formerly in */
142     { "ie", "*Interlingue" },
143     { "ik", "*Inupiak" },
144     { "in", "*Indonesian" },                               /* replaced by id */
145     { "is", "Islenska" },
146     { "it", "Italiano" },
147     { "iu", "*Inuktitut" },
148     { "iw", "*Hebrew" },                                   /* replaced by he */
149     { "ja", "*Japanese" },
150     { "ji", "*Yiddish" },                                  /* replaced by yi */
151     { "jw", "*Javanese" },
152     { "ka", "*Georgian" },
153     { "kk", "*Kazakh" },
154     { "kl", "*Greenlandic" },
155     { "km", "*Cambodian" },
156     { "kn", "*Kannada" },
157     { "ko", "*Korean" },
158     { "ks", "*Kashmiri" },
159     { "ku", "*Kurdish" },
160     { "ky", "*Kirghiz" },
161     { "la", "*Latin" },
162     { "ln", "*Lingala" },
163     { "lo", "*Laothian" },
164     { "lt", "*Lithuanian" },
165     { "lv", "*Latvian, Lettish" },
166     { "mg", "*Malagasy" },
167     { "mi", "*Maori" },
168     { "mk", "*Macedonian" },
169     { "ml", "*Malayalam" },
170     { "mn", "*Mongolian" },
171     { "mo", "*Moldavian" },
172     { "mr", "*Marathi" },
173     { "ms", "*Malay" },
174     { "mt", "*Maltese" },
175     { "my", "*Burmese" },
176     { "na", "*Nauru" },
177     { "ne", "*Nepali" },
178     { "nl", "Nederlands" },
179     { "no", "Norsk" },
180     { "oc", "*Occitan" },
181     { "om", "*(Afan) Oromo" },
182     { "or", "*Oriya" },
183     { "pa", "*Punjabi" },
184     { "pl", "*Polish" },
185     { "ps", "*Pashto, Pushto" },
186     { "pt", "Portugues" },
187     { "qu", "*Quechua" },
188     { "rm", "*Rhaeto-Romance" },
189     { "rn", "*Kirundi" },
190     { "ro", "*Romanian"  },
191     { "ru", "*Russian" },
192     { "rw", "*Kinyarwanda" },
193     { "sa", "*Sanskrit" },
194     { "sd", "*Sindhi" },
195     { "sg", "*Sangho" },
196     { "sh", "*Serbo-Croatian" },
197     { "si", "*Sinhalese" },
198     { "sk", "*Slovak" },
199     { "sl", "*Slovenian" },
200     { "sm", "*Samoan" },
201     { "sn", "*Shona"  },
202     { "so", "*Somali" },
203     { "sq", "*Albanian" },
204     { "sr", "*Serbian" },
205     { "ss", "*Siswati" },
206     { "st", "*Sesotho" },
207     { "su", "*Sundanese" },
208     { "sv", "Svenska" },
209     { "sw", "*Swahili" },
210     { "ta", "*Tamil" },
211     { "te", "*Telugu" },
212     { "tg", "*Tajik" },
213     { "th", "*Thai" },
214     { "ti", "*Tigrinya" },
215     { "tk", "*Turkmen" },
216     { "tl", "*Tagalog" },
217     { "tn", "*Setswana" },
218     { "to", "*Tonga" },
219     { "tr", "*Turkish" },
220     { "ts", "*Tsonga" },
221     { "tt", "*Tatar" },
222     { "tw", "*Twi" },
223     { "ug", "*Uighur" },
224     { "uk", "*Ukrainian" },
225     { "ur", "*Urdu" },
226     { "uz", "*Uzbek" },
227     { "vi", "*Vietnamese" },
228     { "vo", "*Volapuk" },
229     { "wo", "*Wolof" },
230     { "xh", "*Xhosa" },
231     { "yi", "*Yiddish" },                                     /* formerly ji */
232     { "yo", "*Yoruba" },
233     { "za", "*Zhuang" },
234     { "zh", "*Chinese" },
235     { "zu", "*Zulu" },
236     { "\0", "" }
237 };
238
239 /*****************************************************************************
240  * Local prototypes
241  *****************************************************************************/
242 static int  DVDProbe    ( probedata_t *p_data );
243 static int  DVDCheckCSS ( struct input_thread_s * );
244 static int  DVDRead     ( struct input_thread_s *, data_packet_t ** );
245 static void DVDInit     ( struct input_thread_s * );
246 static void DVDEnd      ( struct input_thread_s * );
247 static void DVDSeek     ( struct input_thread_s *, off_t );
248 static int  DVDSetArea  ( struct input_thread_s *, int, int, int, int );
249 static int  DVDRewind   ( struct input_thread_s * );
250
251 /*****************************************************************************
252  * Functions exported as capabilities. They are declared as static so that
253  * we don't pollute the namespace too much.
254  *****************************************************************************/
255 void _M( input_getfunctions )( function_list_t * p_function_list )
256 {
257 #define input p_function_list->functions.input
258     p_function_list->pf_probe = DVDProbe;
259     input.pf_init             = DVDInit;
260     input.pf_open             = input_FileOpen;
261     input.pf_close            = input_FileClose;
262     input.pf_end              = DVDEnd;
263     input.pf_read             = DVDRead;
264     input.pf_set_area         = DVDSetArea;
265     input.pf_demux            = input_DemuxPS;
266     input.pf_new_packet       = DVDNewPacket;
267     input.pf_new_pes          = DVDNewPES;
268     input.pf_delete_packet    = DVDDeletePacket;
269     input.pf_delete_pes       = DVDDeletePES;
270     input.pf_rewind           = DVDRewind;
271     input.pf_seek             = DVDSeek;
272 #undef input
273 }
274
275 /*
276  * Local tools to decode some data in ifo
277  */
278
279 /*****************************************************************************
280  * Language: gives the long language name from the two-letters ISO-639 code
281  *****************************************************************************/
282 static char * Language( u16 i_code )
283 {
284     int     i = 0;
285
286     while( memcmp( lang_tbl[i].p_code, &i_code, 2 ) &&
287            lang_tbl[i].p_lang_long[0] )
288     {
289         i++;
290     }
291
292     return lang_tbl[i].p_lang_long;
293 }
294
295 /*
296  * Data reading functions
297  */
298
299 /*****************************************************************************
300  * DVDProbe: verifies that the stream is a PS stream
301  *****************************************************************************/
302 static int DVDProbe( probedata_t *p_data )
303 {
304     input_thread_t * p_input = (input_thread_t *)p_data;
305
306     char * psz_name = p_input->p_source;
307     int i_handle;
308     int i_score = 5;
309
310     if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
311     {
312         return( 999 );
313     }
314
315     if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
316     {
317         /* If the user specified "dvd:" then it's probably a DVD */
318         i_score = 100;
319         psz_name += 4;
320     }
321
322     i_handle = open( psz_name, 0 );
323     if( i_handle == -1 )
324     {
325         return( 0 );
326     }
327     close( i_handle );
328
329     return( i_score );
330 }
331
332 /*****************************************************************************
333  * DVDCheckCSS: check the stream
334  *****************************************************************************/
335 static int DVDCheckCSS( input_thread_t * p_input )
336 {
337     return CSSTest( p_input->i_handle );
338 }
339
340 /*****************************************************************************
341  * DVDCellToOff: synchronize navigation maps with current offset
342  *****************************************************************************/
343 static int DVDCellToOff( thread_dvd_data_t * p_dvd )
344 {
345
346
347     return 0;
348 }
349
350 /*****************************************************************************
351  * DVDFindCell: from cell index in adress map from index in information table.
352  *****************************************************************************/
353 static int DVDFindCell( thread_dvd_data_t * p_dvd, int i_index )
354 {
355     pgc_t *              p_pgc;
356     int                  i_cell;
357
358     p_pgc = &p_dvd->ifo.vts.pgci_ti.p_srp[p_dvd->i_vts_title-1].pgc;
359     i_cell = 0;
360
361     while( ( p_pgc->p_cell_pos_inf[i_index].i_vob_id >
362              p_dvd->ifo.vts.c_adt.p_cell_inf[i_cell].i_vob_id ) &&
363              p_dvd->ifo.vts.c_adt.i_cell_nb > i_cell )
364     {
365         //if( p_dvd->ifo.vts.c_adt.p_cell_inf[i_cell+1].i_esector*DVD_LB_SIZE >
366         //    p_dvd->i_start ) 
367         {
368             i_cell++;
369         }
370     }
371     while( ( p_pgc->p_cell_pos_inf[i_index].i_cell_id >
372              p_dvd->ifo.vts.c_adt.p_cell_inf[i_cell].i_cell_id ) &&
373              p_dvd->ifo.vts.c_adt.i_cell_nb > i_cell )
374     {
375         //if( p_dvd->ifo.vts.c_adt.p_cell_inf[i_cell+1].i_esector*DVD_LB_SIZE >
376         //    p_dvd->i_start ) 
377         {
378             i_cell++;
379         }
380     }
381
382     return i_cell;
383 }
384
385 /*****************************************************************************
386  * DVDChapterSelect: find the cell corresponding to requested chapter
387  * When called to find chapter 1, also sets title size and end.
388  *****************************************************************************/
389 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
390 {
391     pgc_t *              p_pgc;
392     int                  i_start_cell;
393     int                  i_end_cell;
394     int                  i_index;
395     int                  i_last_chapter;
396
397     p_pgc = &p_dvd->ifo.vts.pgci_ti.p_srp[p_dvd->i_vts_title-1].pgc;
398
399     /* Find cell index in Program chain for current chapter */
400     i_index = p_pgc->prg_map.pi_entry_cell[i_chapter-1] - 1;
401
402     /* Search for cell_index in cell adress_table */
403     i_start_cell = DVDFindCell( p_dvd, i_index );
404
405 /*    intf_WarnMsg( 3, "DVD: Cell: %d vob id: %d cell id: %d", i_start_cell, p_dvd->ifo.vts.c_adt.p_cell_inf[i_start_cell].i_vob_id, p_dvd->ifo.vts.c_adt.p_cell_inf[i_start_cell].i_cell_id );*/
406
407     /* start is : beginning of vts + offset to vobs + offset to vob x */
408     p_dvd->i_start = p_dvd->ifo.vts.i_pos + DVD_LB_SIZE *
409             (off_t)( p_dvd->ifo.vts.mat.i_tt_vobs_ssector +
410                      p_dvd->ifo.vts.c_adt.p_cell_inf[i_start_cell].i_ssector );
411
412     /* compute size of the stream */
413     if( i_chapter == 1 )
414     {
415         i_last_chapter =
416                 p_dvd->ifo.vmg.ptt_srpt.p_tts[p_dvd->i_title-1].i_ptt_nb;
417         i_index = p_pgc->prg_map.pi_entry_cell[i_last_chapter - 1] - 1;
418                   
419 /*        intf_WarnMsg( 3, "last: %d index: %d", i_last_chapter, i_index );*/
420
421         i_end_cell = DVDFindCell( p_dvd, i_index );
422
423 /*        intf_WarnMsg( 3, "DVD: Cell: %d vob id: %d cell id: %d", i_end_cell, p_dvd->ifo.vts.c_adt.p_cell_inf[i_end_cell].i_vob_id, p_dvd->ifo.vts.c_adt.p_cell_inf[i_end_cell].i_cell_id );*/
424
425         p_dvd->i_size = (off_t)DVD_LB_SIZE *
426             ( p_dvd->ifo.vts.c_adt.p_cell_inf[i_end_cell].i_esector -
427               p_dvd->ifo.vts.c_adt.p_cell_inf[i_start_cell].i_ssector + 1 );
428         intf_WarnMsg( 3, "DVD: Start cell: %d End Cell: %d",
429                                             i_start_cell, i_end_cell );
430     }
431
432     return 0;
433 }
434
435 /*****************************************************************************
436  * DVDSetArea: initialize input data for title x, chapter y.
437  * It should be called for each user navigation request, and to change
438  * audio or sub-picture streams.
439  * ---
440  * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
441  * i_audio, i_spu start from 1 ; 0 means off.
442  * A negative value for an argument means it does not change
443  *****************************************************************************/
444 static int DVDSetArea( input_thread_t * p_input,
445                        int i_title, int i_chapter,
446                        int i_audio, int i_spu )
447 {
448     thread_dvd_data_t *  p_method;
449     es_descriptor_t *    p_es;
450     int                  i_index;
451     int                  i_nb;
452     u16                  i_id;
453     u8                   i_ac3;
454     u8                   i_mpeg;
455     u8                   i_sub_pic;
456     u8                   i;
457     boolean_t            b_last;
458     
459     p_method = (thread_dvd_data_t*)p_input->p_plugin_data;
460
461     vlc_mutex_lock( &p_input->stream.stream_lock );
462
463     if( i_title >= 0 )
464     {
465
466         /*
467          *  We have to load all title information
468          */
469
470         /* Change the default area */
471         p_input->stream.p_selected_area = p_input->stream.pp_areas[i_title];
472
473         p_method->i_title = i_title;
474
475         /* title position inside the selected vts */
476         p_method->i_vts_title =
477                 p_method->ifo.vmg.ptt_srpt.p_tts[i_title-1].i_vts_ttn;
478
479         /* vts number */
480         p_method->ifo.i_title =
481                 p_method->ifo.vmg.ptt_srpt.p_tts[i_title-1].i_tts_nb;
482
483         /* ifo vts */
484         IfoReadVTS( &(p_method->ifo) );
485         intf_WarnMsg( 2, "ifo info: vts initialized" );
486     
487         /* css title key for current vts */
488         if( p_method->b_encrypted )
489         {
490             p_method->p_css->i_title =
491                     p_method->ifo.vmg.ptt_srpt.p_tts[i_title-1].i_tts_nb;
492             p_method->p_css->i_title_pos =
493                     p_method->ifo.vts.i_pos +
494                     p_method->ifo.vts.mat.i_tt_vobs_ssector * DVD_LB_SIZE;
495             CSSGetKey( p_method->p_css );
496             intf_WarnMsg( 2, "css info: vts key initialized" );
497         }
498     
499         /*
500          * Set selected title start and size
501          */
502         DVDChapterSelect( p_method, 1 );
503     
504         p_method->i_start =
505                     lseek( p_input->i_handle, p_method->i_start, SEEK_SET );
506
507         intf_WarnMsg( 2, "dvd info: title: %d", i_title );
508         intf_WarnMsg( 2, "dvd info: vobstart at: %lld", p_method->i_start );
509         intf_WarnMsg( 2, "dvd info: stream size: %lld", p_method->i_size );
510         intf_WarnMsg( 2, "dvd info: number of chapters: %d",
511                             p_input->stream.p_selected_area->i_part_nb );
512
513         /* Area definition */
514         p_input->stream.p_selected_area->i_start = p_method->i_start;
515         p_input->stream.p_selected_area->i_size = p_method->i_size;
516
517         /*
518          * Destroy obsolete ES by reinitializing program 0
519          * and find all ES in title with ifo data
520          */
521         if( p_input->stream.pp_programs != NULL )
522         {
523             input_DelProgram( p_input, p_input->stream.pp_programs[0] );
524         }
525
526         input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
527
528         p_es = NULL;
529
530         /* ES 0 -> video MPEG2 */
531         p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
532         p_es->i_stream_id = 0xe0;
533         p_es->i_type = MPEG2_VIDEO_ES;
534         input_SelectES( p_input, p_es );
535         intf_WarnMsg( 1, "dvd info: video MPEG2 stream" );
536     
537         /* Audio ES, in the order they appear in .ifo */
538         i_nb = p_method->ifo.vts.mat.i_audio_nb;
539     
540         i_ac3 = 0x80;
541         i_mpeg = 0xc0;
542
543         for( i = 1 ; i <= i_nb ; i++ )
544         {
545
546 #if 0
547     fprintf( stderr, "Audio %d: %x %x %x %x %x %x %x\n", i,
548             p_method->ifo.vts.mat.p_audio_atrt[i].i_num_channels,
549             p_method->ifo.vts.mat.p_audio_atrt[i].i_coding_mode,
550             p_method->ifo.vts.mat.p_audio_atrt[i].i_multichannel_extension,
551             p_method->ifo.vts.mat.p_audio_atrt[i].i_type,
552             p_method->ifo.vts.mat.p_audio_atrt[i].i_appl_mode,
553             p_method->ifo.vts.mat.p_audio_atrt[i].i_foo,
554             p_method->ifo.vts.mat.p_audio_atrt[i].i_bar );
555 #endif
556
557             switch( p_method->ifo.vts.mat.p_audio_atrt[i].i_coding_mode )
558             {
559             case 0x00:              /* AC3 */
560                 i_id = ( ( 0x7f + i ) << 8 ) | 0xbd;
561                 p_es = input_AddES( p_input,
562                                     p_input->stream.pp_programs[0], i_id, 0 );
563                 p_es->i_stream_id = 0xbd;
564                 p_es->i_type = AC3_AUDIO_ES;
565                 p_es->b_audio = 1;
566                 strcpy( p_es->psz_desc, Language( hton16(
567                     p_method->ifo.vts.mat.p_audio_atrt[i-1].i_lang_code ) ) ); 
568
569                 break;
570             case 0x02:
571             case 0x03:              /* MPEG audio */
572                 i_id = 0xbf + i;
573                 p_es = input_AddES( p_input,
574                                     p_input->stream.pp_programs[0], i_id, 0 );
575                 p_es->i_stream_id = i_id;
576                 p_es->i_type = MPEG2_AUDIO_ES;
577                 p_es->b_audio = 1;
578                 strcpy( p_es->psz_desc, Language( hton16(
579                     p_method->ifo.vts.mat.p_audio_atrt[i-1].i_lang_code ) ) ); 
580
581                 break;
582             case 0x04:              /* LPCM */
583                 i_id = 0;
584                 intf_ErrMsg( "dvd error: LPCM audio not handled yet" );
585                 break;
586             case 0x06:              /* DTS */
587                 i_id = 0;
588                 intf_ErrMsg( "dvd error: DTS audio not handled yet" );
589                 break;
590             default:
591                 i_id = 0;
592                 intf_ErrMsg( "dvd error: unkown audio" );
593             }
594         
595             intf_WarnMsg( 1, "dvd info: audio stream %d %s\t(0x%x)",
596                           i, p_es->psz_desc, i_id );
597         }
598     
599         /* Sub Picture ES */
600         i_nb = p_method->ifo.vts.mat.i_subpic_nb;
601     
602         b_last = 0;
603         i_sub_pic = 0x20;
604         for( i = 1 ; i <= i_nb ; i++ )
605         {
606             if( !b_last )
607             {
608                 i_id = ( i_sub_pic++ << 8 ) | 0xbd;
609                 p_es = input_AddES( p_input,
610                                     p_input->stream.pp_programs[0], i_id, 0 );
611                 p_es->i_stream_id = 0xbd;
612                 p_es->i_type = DVD_SPU_ES;
613                 strcpy( p_es->psz_desc, Language( hton16(
614                     p_method->ifo.vts.mat.p_subpic_atrt[i-1].i_lang_code ) ) ); 
615                 intf_WarnMsg( 1, "dvd info: spu stream %d %s\t(0x%x)",
616                               i, p_es->psz_desc, i_id );
617     
618                 /* The before the last spu has a 0x0 prefix */
619                 b_last =
620                     ( p_method->ifo.vts.mat.p_subpic_atrt[i].i_prefix == 0 ); 
621             }
622         }
623
624     } // i_title >= 0
625
626     /*
627      * Select requested ES
628      */
629     if( ( i_audio >= 0 ) || ( i_title >= 0 ) )
630     {
631
632         /* Audio: we check it is in the range and
633          * default it to the first if not */
634         if( i_audio > p_method->ifo.vts.mat.i_audio_nb )
635         {
636             i_audio = 1;
637         }
638
639         p_es = p_input->stream.pp_programs[0]->pp_es[i_audio];
640
641         /* We can only have one audio channel */
642         /* Look for a preselected one */
643         i_index = -1;
644         for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
645         {
646             if( p_input->stream.pp_selected_es[i]->b_audio )
647             {
648                 i_index = i;
649                 break;
650             }
651         }
652
653         if( i_index != -1 )
654         {
655             
656             if( p_input->stream.pp_selected_es[i_index] != p_es )
657             {
658                 input_UnselectES( p_input,
659                                   p_input->stream.pp_selected_es[i_index] );
660                 input_SelectES( p_input, p_es );
661                 intf_WarnMsg( 1, "dvd info: audio %d selected -> %s (0x%x)",
662                               i_audio, p_es->psz_desc, p_es->i_id );
663             }
664         }
665         else
666         {
667             input_SelectES( p_input, p_es );
668             intf_WarnMsg( 1, "dvd info: audio %d selected -> %s (0x%x)",
669                           i_audio, p_es->psz_desc, p_es->i_id );
670         }
671     }
672
673     if( ( i_spu >= 0 ) || ( i_title >= 0 ) )
674     {
675         /* For spu: no one if none or a not existed one requested */
676         if( ( i_spu <= p_method->ifo.vts.mat.i_subpic_nb ) && ( i_spu > 0 ) )
677         {
678             input_SelectES( p_input, ( p_es = p_input->stream.pp_programs[0]->
679                 pp_es[ i_spu + p_method->ifo.vts.mat.i_audio_nb ] ) );
680     
681             intf_WarnMsg( 1, "dvd info: spu   %d selected -> %s (0x%x)",
682                           i_spu, p_es->psz_desc, p_es->i_id );
683         }
684     }
685
686     /*
687      * Chapter selection
688      */
689
690     if( ( i_chapter > 0 ) &&
691         ( i_chapter <= p_input->stream.p_selected_area->i_part_nb ) )
692     {
693         DVDChapterSelect( p_method, i_chapter );
694
695         p_input->stream.p_selected_area->i_part = i_chapter; 
696
697         DVDSeek( p_input, p_method->i_start -
698                           p_input->stream.p_selected_area->i_start );
699
700         intf_WarnMsg( 2, "dvd info: chapter %d start at: %lld", i_chapter,
701                                     p_input->stream.p_selected_area->i_tell );
702     }
703
704     /* No PSM to read in DVD mode, we already have all information */
705     p_input->stream.pp_programs[0]->b_is_ok = 1;
706
707     vlc_mutex_unlock( &p_input->stream.stream_lock );
708
709     return 0;
710 }
711
712 /*****************************************************************************
713  * DVDInit: initializes DVD structures
714  *****************************************************************************/
715 static void DVDInit( input_thread_t * p_input )
716 {
717     thread_dvd_data_t *  p_method;
718     int                  i_title;
719     int                  i_chapter;
720     int                  i_audio;
721     int                  i_spu;
722     int                  i;
723
724     if( (p_method = malloc( sizeof(thread_dvd_data_t) )) == NULL )
725     {
726         intf_ErrMsg( "Out of memory" );
727         p_input->b_error = 1;
728         return;
729     }
730
731     p_input->p_plugin_data = (void *)p_method;
732     p_input->p_method_data = NULL;
733
734     p_method->i_fd = p_input->i_handle;
735
736     p_method->i_block_once = 32;
737     p_input->i_read_once = 128;
738
739     p_method->b_encrypted = DVDCheckCSS( p_input );
740
741     lseek( p_input->i_handle, 0, SEEK_SET );
742
743     /* Reading structures initialisation */
744     p_input->p_method_data =
745     DVDNetlistInit( 4096, 16384, 4096, DVD_LB_SIZE, p_method->i_block_once ); 
746     intf_WarnMsg( 2, "dvd info: netlist initialized" );
747
748     /* Ifo initialisation */
749     p_method->ifo = IfoInit( p_input->i_handle );
750     intf_WarnMsg( 2, "ifo info: vmg initialized" );
751
752     /* CSS initialisation */
753     if( p_method->b_encrypted )
754     {
755         p_method->p_css = CSSInit( p_input->i_handle );
756
757         if( p_method->p_css == NULL )
758         {
759             intf_ErrMsg( "css error: fatal failure" );
760             p_input->b_error = 1;
761             return;
762         }
763
764         intf_WarnMsg( 2, "css info: initialized" );
765     }
766
767     /* Initialize ES structures */
768     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
769
770     /* Set stream and area data */
771     vlc_mutex_lock( &p_input->stream.stream_lock );
772
773 #define srpt p_method->ifo.vmg.ptt_srpt
774     intf_WarnMsg( 2, "dvd info: number of titles: %d", srpt.i_ttu_nb );
775
776 #define area p_input->stream.pp_areas
777     /* We start from 1 here since area 0 is reserved for video_ts.vob */
778     for( i = 1 ; i <= srpt.i_ttu_nb ; i++ )
779     {
780         input_AddArea( p_input );
781
782         /* Should not be so simple eventually :
783          * are title Program Chains, or still something else ? */
784         area[i]->i_id = i;
785
786         /* Absolute start offset and size 
787          * We can only set that with vts ifo, so we do it during the
788          * first call to DVDSetArea */
789         area[i]->i_start = 0;
790         area[i]->i_size = 0;
791
792         /* Number of chapter */
793         area[i]->i_part_nb = srpt.p_tts[i-1].i_ptt_nb;
794         area[i]->i_part = 1;
795
796         /* Offset to vts_i_0.ifo */
797         area[i]->i_plugin_data = p_method->ifo.i_off +
798                                ( srpt.p_tts[i-1].i_ssector * DVD_LB_SIZE );
799     }   
800 #undef area
801
802     vlc_mutex_unlock( &p_input->stream.stream_lock );
803
804     /* Get requested title - if none try to find one where is the movie */
805     i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
806     if( i_title <= 0 || i_title > srpt.i_ttu_nb )
807     {
808         i_title = 1;
809     }
810 #undef srpt
811
812     /* Get requested chapter - if none defaults to first one */
813     i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
814     if( i_chapter <= 0 )
815     {
816         i_chapter = 1;
817     }
818
819     /* For audio: first one if none or a not existing one specified */
820     i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
821     if( i_audio <= 0 )
822     {
823         main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
824         i_audio = 1;
825     }
826
827     i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
828
829     DVDSetArea( p_input, i_title, i_chapter, i_audio, i_spu );
830
831     return;
832 }
833
834 /*****************************************************************************
835  * DVDEnd: frees unused data
836  *****************************************************************************/
837 static void DVDEnd( input_thread_t * p_input )
838 {
839     thread_dvd_data_t *     p_method;
840     dvd_netlist_t *         p_netlist;
841
842     p_method = (thread_dvd_data_t*)p_input->p_plugin_data;
843     p_netlist = (dvd_netlist_t *)p_input->p_method_data;
844
845     CSSEnd( p_method->p_css );
846 //    IfoEnd( p_method->p_ifo ) );
847     free( p_method );
848     DVDNetlistEnd( p_netlist );
849 }
850
851 /*****************************************************************************
852  * DVDRead: reads data packets into the netlist.
853  *****************************************************************************
854  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
855  * EOF.
856  *****************************************************************************/
857 static int DVDRead( input_thread_t * p_input,
858                     data_packet_t ** pp_packets )
859 {
860     thread_dvd_data_t *     p_method;
861     dvd_netlist_t *         p_netlist;
862     struct iovec *          p_vec;
863     struct data_packet_s *  pp_data[p_input->i_read_once];
864     u8 *                    pi_cur;
865     int                     i_packet_size;
866     int                     i_iovec;
867     int                     i_packet;
868     int                     i_pos;
869
870     p_method = (thread_dvd_data_t *)p_input->p_plugin_data;
871     p_netlist = (dvd_netlist_t *)p_input->p_method_data;
872
873     /* Get an iovec pointer */
874     if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
875     {
876         intf_ErrMsg( "DVD: read error" );
877         return -1;
878     }
879
880     /* Reads from DVD */
881     readv( p_input->i_handle, p_vec, p_method->i_block_once );
882
883     /* Update netlist indexes */
884     DVDMviovec( p_netlist, p_method->i_block_once, pp_data );
885     i_packet = 0;
886
887     /* Read headers to compute payload length */
888     for( i_iovec = 0 ; i_iovec < p_method->i_block_once ; i_iovec++ )
889     {
890         if( p_method->b_encrypted )
891         {
892             CSSDescrambleSector( p_method->p_css->pi_title_key, 
893                                  p_vec[i_iovec].iov_base );
894             ((u8*)(p_vec[i_iovec].iov_base))[0x14] &= 0x8F;
895         }
896
897         i_pos = 0;
898
899         while( i_pos < p_netlist->i_buffer_size )
900         {
901             pi_cur = (u8*)(p_vec[i_iovec].iov_base + i_pos);
902
903             /*default header */
904             if( U32_AT( pi_cur ) != 0x1BA )
905             {
906                 /* That's the case for all packets, except pack header. */
907                 i_packet_size = U16_AT( pi_cur + 4 );
908                 pp_packets[i_packet] = DVDNewPtr( p_netlist );
909             }
910             else
911             {
912                 /* Pack header. */
913                 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
914                 {
915                     /* MPEG-2 */
916                     i_packet_size = 8;
917                 }
918                 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
919                 {
920                     /* MPEG-1 */
921                     i_packet_size = 6;
922                 }
923                 else
924                 {
925                     intf_ErrMsg( "Unable to determine stream type" );
926                     return( -1 );
927                 }
928
929                 pp_packets[i_packet] = pp_data[i_iovec];
930
931             }
932
933             (*pp_data[i_iovec]->pi_refcount)++;
934
935             pp_packets[i_packet]->pi_refcount = pp_data[i_iovec]->pi_refcount;
936
937             pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
938
939             pp_packets[i_packet]->p_payload_start =
940                     pp_packets[i_packet]->p_buffer + i_pos;
941
942             pp_packets[i_packet]->p_payload_end =
943                     pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
944
945             i_packet++;
946             i_pos += i_packet_size + 6;
947         }
948     }
949
950     pp_packets[i_packet] = NULL;
951
952     vlc_mutex_lock( &p_input->stream.stream_lock );
953     p_input->stream.p_selected_area->i_tell +=
954                                         p_method->i_block_once *DVD_LB_SIZE;
955     vlc_mutex_unlock( &p_input->stream.stream_lock );
956
957     return( 0 );
958 }
959
960
961 /*****************************************************************************
962  * DVDRewind : reads a stream backward
963  *****************************************************************************/
964 static int DVDRewind( input_thread_t * p_input )
965 {
966     return( -1 );
967 }
968
969 /*****************************************************************************
970  * DVDSeek : Goes to a given position on the stream ; this one is used by the 
971  * input and translate chronological position from input to logical postion
972  * on the device
973  *****************************************************************************/
974 static void DVDSeek( input_thread_t * p_input, off_t i_off )
975 {
976     thread_dvd_data_t *     p_method;
977     off_t                   i_pos;
978     
979     p_method = ( thread_dvd_data_t * )p_input->p_plugin_data;
980
981     /* We have to take care of offset of beginning of title */
982     i_pos = i_off + p_input->stream.p_selected_area->i_start;
983
984     /* With DVD, we have to be on a sector boundary */
985     i_pos = i_pos & (~0x7ff);
986
987     i_pos = lseek( p_input->i_handle, i_pos, SEEK_SET );
988
989     p_input->stream.p_selected_area->i_tell = i_pos -
990                                     p_input->stream.p_selected_area->i_start;
991
992     return;
993 }
994
995
996