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