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