]> git.sesse.net Git - vlc/blob - plugins/dvd/input_dvd.c
f21bdc64aa4b7887912b946608341a4641d8f07c
[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.31 2001/03/07 10:31:10 stef Exp $
14  *
15  * Author: Stéphane Borel <stef@via.ecp.fr>
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 2 of the License, or
20  * (at your option) any later version.
21  * 
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
30  *****************************************************************************/
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35 #include "defs.h"
36
37 #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 *, struct input_area_s * );
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, input_area_t * p_area )
445 {
446     thread_dvd_data_t *  p_dvd;
447     es_descriptor_t *    p_es;
448     int                  i_nb;
449     u16                  i_id;
450     u8                   i_ac3;
451     u8                   i_mpeg;
452     u8                   i_sub_pic;
453     u8                   i;
454     boolean_t            b_last;
455
456     p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
457
458     vlc_mutex_lock( &p_input->stream.stream_lock );
459
460     if( p_area != p_input->stream.p_selected_area )
461     {
462
463         /*
464          *  We have to load all title information
465          */
466         /* Change the default area */
467         p_input->stream.p_selected_area = p_input->stream.pp_areas[p_area->i_id];
468
469         /* title number: it is not vts nb! */
470         p_dvd->i_title = p_area->i_id;
471
472         /* title position inside the selected vts */
473         p_dvd->i_vts_title =
474                     p_dvd->ifo.vmg.ptt_srpt.p_tts[p_dvd->i_title-1].i_vts_ttn;
475
476         /* vts number */
477         p_dvd->ifo.i_title = p_dvd->i_title;
478
479         /* ifo vts */
480         IfoReadVTS( &(p_dvd->ifo) );
481         intf_WarnMsg( 2, "ifo info: vts initialized" );
482     
483         /* css title key for current vts */
484         if( p_dvd->b_encrypted )
485         {
486             p_dvd->p_css->i_title =
487                     p_dvd->ifo.vmg.ptt_srpt.p_tts[p_dvd->i_title-1].i_tts_nb;
488             p_dvd->p_css->i_title_pos =
489                     p_dvd->ifo.vts.i_pos +
490                     p_dvd->ifo.vts.mat.i_tt_vobs_ssector * DVD_LB_SIZE;
491             CSSGetKey( p_dvd->p_css );
492             intf_WarnMsg( 2, "css info: vts key initialized" );
493         }
494     
495         /*
496          * Set selected title start and size
497          */
498         
499         /* title set offset */
500         p_dvd->i_title_start = p_dvd->ifo.vts.i_pos + DVD_LB_SIZE *
501                       (off_t)( p_dvd->ifo.vts.mat.i_tt_vobs_ssector );
502
503         /* last video cell */
504         p_dvd->i_cell = -1;
505         p_dvd->i_prg_cell = -1 +
506             p_dvd->ifo.vts.pgci_ti.p_srp[p_dvd->i_vts_title-1].pgc.i_cell_nb;
507         DVDFindSector( p_dvd );
508
509         /* temporary hack to fix size in some dvds */
510         if( p_dvd->i_cell >= p_dvd->ifo.vts.c_adt.i_cell_nb )
511         {
512             p_dvd->i_cell = p_dvd->ifo.vts.c_adt.i_cell_nb - 1;
513         }
514
515         p_dvd->i_size = DVD_LB_SIZE *
516           (off_t)( p_dvd->ifo.vts.c_adt.p_cell_inf[p_dvd->i_cell].i_esector );
517
518         DVDChapterSelect( p_dvd, 1 );
519
520         p_dvd->i_size -= (off_t)( p_dvd->i_sector + 1 ) *DVD_LB_SIZE;
521
522         lseek( p_input->i_handle, p_dvd->i_start, SEEK_SET );
523
524
525         intf_WarnMsg( 2, "dvd info: title: %d", p_dvd->i_title );
526         intf_WarnMsg( 2, "dvd info: vobstart at: %lld", p_dvd->i_start );
527         intf_WarnMsg( 2, "dvd info: stream size: %lld", p_dvd->i_size );
528         intf_WarnMsg( 2, "dvd info: number of chapters: %d",
529                    p_dvd->ifo.vmg.ptt_srpt.p_tts[p_dvd->i_title-1].i_ptt_nb );
530
531 /*        intf_WarnMsg( 3, "last: %d index: %d", i_last_chapter, i_index );*/
532
533
534 /*        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 );*/
535
536         /* Area definition */
537         p_input->stream.p_selected_area->i_start = p_dvd->i_start;
538         p_input->stream.p_selected_area->i_size = p_dvd->i_size;
539
540         /*
541          * Destroy obsolete ES by reinitializing program 0
542          * and find all ES in title with ifo data
543          */
544         if( p_input->stream.pp_programs != NULL )
545         {
546             /* We don't use input_EndStream here since
547              * we keep area structures */
548
549             for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
550             {
551                 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
552             }
553
554             input_DelProgram( p_input, p_input->stream.pp_programs[0] );
555
556             free( p_input->stream.pp_selected_es );
557             p_input->stream.pp_selected_es = NULL;
558             p_input->stream.i_selected_es_number = 0;
559         }
560
561         input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
562
563         p_es = NULL;
564
565         /* ES 0 -> video MPEG2 */
566         p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
567         p_es->i_stream_id = 0xe0;
568         p_es->i_type = MPEG2_VIDEO_ES;
569         input_SelectES( p_input, p_es );
570         intf_WarnMsg( 1, "dvd info: video MPEG2 stream" );
571
572     
573         /* Audio ES, in the order they appear in .ifo */
574         i_nb = p_dvd->ifo.vts.mat.i_audio_nb;
575     
576         i_ac3 = 0x7f;
577         i_mpeg = 0xc0;
578
579         for( i = 1 ; i <= i_nb ; i++ )
580         {
581
582 #if 0
583     fprintf( stderr, "Audio %d: %x %x %x %x %x %x %x %x %x %x %x %x\n", i,
584             p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_num_channels,
585             p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_coding_mode,
586             p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_multichannel_extension,
587             p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_type,
588             p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_appl_mode,
589             p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_foo,
590             p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_bar,
591             p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_appl_mode,
592             p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_quantization,
593             p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_sample_freq,
594             p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_lang_code,
595             p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_caption );
596 #endif
597
598             switch( p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_coding_mode )
599             {
600             case 0x00:              /* AC3 */
601                 i_id = ( ( i_ac3 + i ) << 8 ) | 0xbd;
602                 p_es = input_AddES( p_input,
603                                     p_input->stream.pp_programs[0], i_id, 0 );
604                 p_es->i_stream_id = 0xbd;
605                 p_es->i_type = AC3_AUDIO_ES;
606                 p_es->b_audio = 1;
607                 strcpy( p_es->psz_desc, Language( hton16(
608                     p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_lang_code ) ) ); 
609
610                 intf_WarnMsg( 1, "dvd info: audio stream %d %s\t(0x%x)",
611                               i, p_es->psz_desc, i_id );
612
613                 break;
614             case 0x02:
615             case 0x03:              /* MPEG audio */
616                 i_id = 0xbf + i;
617                 p_es = input_AddES( p_input,
618                                     p_input->stream.pp_programs[0], i_id, 0 );
619                 p_es->i_stream_id = i_id;
620                 p_es->i_type = MPEG2_AUDIO_ES;
621                 p_es->b_audio = 1;
622                 strcpy( p_es->psz_desc, Language( hton16(
623                     p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_lang_code ) ) ); 
624
625                 intf_WarnMsg( 1, "dvd info: audio stream %d %s\t(0x%x)",
626                               i, p_es->psz_desc, i_id );
627
628                 break;
629             case 0x04:              /* LPCM */
630                 i_id = 0;
631                 intf_ErrMsg( "dvd warning: LPCM audio not handled yet" );
632                 break;
633             case 0x06:              /* DTS */
634                 i_id = 0;
635                 i_ac3--;
636                 intf_ErrMsg( "dvd warning: DTS audio not handled yet" );
637                 break;
638             default:
639                 i_id = 0;
640                 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
641                          p_dvd->ifo.vts.mat.p_audio_atrt[i-1].i_coding_mode );
642             }
643         
644         }
645     
646         /* Sub Picture ES */
647         i_nb = p_dvd->ifo.vts.mat.i_subpic_nb;
648     
649         b_last = 0;
650         i_sub_pic = 0x20;
651         for( i = 1 ; i <= i_nb ; i++ )
652         {
653             if( !b_last )
654             {
655                 i_id = ( i_sub_pic++ << 8 ) | 0xbd;
656                 p_es = input_AddES( p_input,
657                                     p_input->stream.pp_programs[0], i_id, 0 );
658                 p_es->i_stream_id = 0xbd;
659                 p_es->i_type = DVD_SPU_ES;
660                 p_es->b_spu = 1;
661                 strcpy( p_es->psz_desc, Language( hton16(
662                     p_dvd->ifo.vts.mat.p_subpic_atrt[i-1].i_lang_code ) ) ); 
663                 intf_WarnMsg( 1, "dvd info: spu stream %d %s\t(0x%x)",
664                               i, p_es->psz_desc, i_id );
665     
666                 /* The before the last spu has a 0x0 prefix */
667                 b_last =
668                     ( p_dvd->ifo.vts.mat.p_subpic_atrt[i].i_prefix == 0 ); 
669             }
670         }
671
672     } // i_title >= 0
673     else
674     {
675         p_area = p_input->stream.p_selected_area;
676     }
677
678     /*
679      * Chapter selection
680      */
681
682     if( ( p_area->i_part > 0 ) &&
683         ( p_area->i_part <= p_area->i_part_nb ) )
684     {
685         DVDChapterSelect( p_dvd, p_area->i_part );
686
687         p_input->stream.p_selected_area->i_tell = p_dvd->i_start -
688                                                   p_area->i_start;
689         p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
690
691         intf_WarnMsg( 2, "dvd info: chapter %d start at: %lld",
692                                     p_area->i_part, p_area->i_tell );
693     }
694
695     /* By default first audio stream and no spu */
696     input_SelectES( p_input, p_input->stream.pp_es[1] );
697
698     /* No PSM to read in DVD mode, we already have all information */
699     p_input->stream.pp_programs[0]->b_is_ok = 1;
700
701     vlc_mutex_unlock( &p_input->stream.stream_lock );
702
703     return 0;
704 }
705
706 /*****************************************************************************
707  * DVDInit: initializes DVD structures
708  *****************************************************************************/
709 static void DVDInit( input_thread_t * p_input )
710 {
711     thread_dvd_data_t *  p_dvd;
712     int                  i_title;
713     int                  i_chapter;
714     int                  i_audio;
715     int                  i_spu;
716     int                  i;
717
718     if( (p_dvd = malloc( sizeof(thread_dvd_data_t) )) == NULL )
719     {
720         intf_ErrMsg( "Out of memory" );
721         p_input->b_error = 1;
722         return;
723     }
724
725     p_input->p_plugin_data = (void *)p_dvd;
726     p_input->p_method_data = NULL;
727
728     p_dvd->i_fd = p_input->i_handle;
729
730     p_dvd->i_block_once = 1;
731     p_input->i_read_once = 8;
732
733     p_dvd->b_encrypted = DVDCheckCSS( p_input );
734
735     lseek( p_input->i_handle, 0, SEEK_SET );
736
737     /* Reading structures initialisation */
738     p_input->p_method_data =
739 //    DVDNetlistInit( 4096, 8192, 4096, DVD_LB_SIZE, p_dvd->i_block_once ); 
740     DVDNetlistInit( 8192, 16384, 16384, DVD_LB_SIZE, p_dvd->i_block_once ); 
741     intf_WarnMsg( 2, "dvd info: netlist initialized" );
742
743     /* Ifo initialisation */
744     p_dvd->ifo = IfoInit( p_input->i_handle );
745     intf_WarnMsg( 2, "ifo info: vmg initialized" );
746
747     /* CSS initialisation */
748     if( p_dvd->b_encrypted )
749     {
750         p_dvd->p_css = CSSInit( p_input->i_handle );
751
752         if( p_dvd->p_css == NULL )
753         {
754             intf_ErrMsg( "css error: fatal failure" );
755             p_input->b_error = 1;
756             return;
757         }
758
759         intf_WarnMsg( 2, "css info: initialized" );
760     }
761
762     /* Initialize ES structures */
763     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
764
765     /* Set stream and area data */
766     vlc_mutex_lock( &p_input->stream.stream_lock );
767
768 #define srpt p_dvd->ifo.vmg.ptt_srpt
769     intf_WarnMsg( 2, "dvd info: number of titles: %d", srpt.i_ttu_nb );
770
771 #define area p_input->stream.pp_areas
772     /* We start from 1 here since area 0 is reserved for video_ts.vob */
773     for( i = 1 ; i <= srpt.i_ttu_nb ; i++ )
774     {
775         input_AddArea( p_input );
776
777         /* Should not be so simple eventually :
778          * are title Program Chains, or still something else ? */
779         area[i]->i_id = i;
780
781         /* Absolute start offset and size 
782          * We can only set that with vts ifo, so we do it during the
783          * first call to DVDSetArea */
784         area[i]->i_start = 0;
785         area[i]->i_size = 0;
786
787         /* Number of chapter */
788         area[i]->i_part_nb = srpt.p_tts[i-1].i_ptt_nb;
789         area[i]->i_part = 1;
790
791         /* Offset to vts_i_0.ifo */
792         area[i]->i_plugin_data = p_dvd->ifo.i_off +
793                                ( srpt.p_tts[i-1].i_ssector * DVD_LB_SIZE );
794     }   
795 #undef area
796
797     vlc_mutex_unlock( &p_input->stream.stream_lock );
798
799     /* Get requested title - if none try to find one where is the movie */
800     i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
801     if( i_title <= 0 || i_title > srpt.i_ttu_nb )
802     {
803         i_title = 1;
804     }
805 #undef srpt
806     /* Get requested chapter - if none defaults to first one */
807     i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
808     if( i_chapter <= 0 )
809     {
810         i_chapter = 1;
811     }
812
813     p_input->stream.pp_areas[i_title]->i_part = i_chapter;
814     DVDSetArea( p_input, p_input->stream.pp_areas[i_title] );
815
816     /* For audio: first one if none or a not existing one specified */
817     i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
818     if( i_audio <= 0 )
819     {
820         main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
821         i_audio = 1;
822     }
823
824     input_ChangeES( p_input, p_input->stream.pp_es[i_audio], 1 );
825
826     i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
827     if( i_spu < 0 )
828     {
829         main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
830         i_spu = 0;
831     }
832
833 //    input_ChangeES( p_input, p_input->stream.pp_es[i_spu], 1 );
834
835
836     return;
837 }
838
839 /*****************************************************************************
840  * DVDEnd: frees unused data
841  *****************************************************************************/
842 static void DVDEnd( input_thread_t * p_input )
843 {
844     thread_dvd_data_t *     p_dvd;
845     dvd_netlist_t *         p_netlist;
846
847     p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
848     p_netlist = (dvd_netlist_t *)p_input->p_method_data;
849
850     CSSEnd( p_dvd->p_css );
851 //    IfoEnd( p_dvd->p_ifo ) );
852     free( p_dvd );
853     DVDNetlistEnd( p_netlist );
854 }
855
856 /*****************************************************************************
857  * DVDRead: reads data packets into the netlist.
858  *****************************************************************************
859  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
860  * EOF.
861  *****************************************************************************/
862 static int DVDRead( input_thread_t * p_input,
863                     data_packet_t ** pp_packets )
864 {
865     thread_dvd_data_t *     p_dvd;
866     dvd_netlist_t *         p_netlist;
867     pgc_t *                 p_pgc;
868     struct iovec *          p_vec;
869     struct data_packet_s *  pp_data[p_input->i_read_once];
870     u8 *                    pi_cur;
871     int                     i_packet_size;
872     int                     i_iovec;
873     int                     i_packet;
874     int                     i_pos;
875     int                     i_read_bytes;
876     int                     i_read_blocks;
877     int                     i_chapter;
878     off_t                   i_off;
879     boolean_t               b_eof;
880
881     p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
882     p_netlist = (dvd_netlist_t *)p_input->p_method_data;
883     p_pgc = &p_dvd->ifo.vts.pgci_ti.p_srp[p_dvd->i_vts_title-1].pgc;
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     /* Get the position of the next cell if we're at cell end */
893 #if 1
894     if( p_dvd->i_sector > p_dvd->i_end_sector )
895     {
896         /* Find cell index in adress map */
897         if( DVDFindSector( p_dvd ) < 0 )
898         {
899             pp_packets[0] = NULL;
900             return 1;
901         }
902
903         /* Position the fd pointer on the right address */
904         i_off = lseek( p_dvd->i_fd,
905                        p_dvd->i_title_start +
906                        (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE,
907                SEEK_SET );
908
909         i_chapter = 0;
910
911         /* update chapter */
912         while( p_pgc->prg_map.pi_entry_cell[i_chapter-1] - 1 <
913                p_dvd->i_prg_cell )
914         {
915             i_chapter++;
916         }
917
918         p_dvd->i_chapter = i_chapter;
919
920 //        vlc_mutex_lock( &p_input->stream.stream_lock );
921
922         p_input->stream.p_selected_area->i_tell = i_off -
923                                     p_input->stream.p_selected_area->i_start;
924         p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
925
926 //        vlc_mutex_unlock( &p_input->stream.stream_lock );
927     }
928
929 #endif
930     /* Reads from DVD */
931     i_read_bytes = readv( p_dvd->i_fd, p_vec, p_dvd->i_block_once );
932     i_read_blocks = ( i_read_bytes + 0x7ff ) >> 11;
933
934     /* Update netlist indexes */
935     DVDMviovec( p_netlist, i_read_blocks, pp_data );
936
937     /* Update global position */
938     p_dvd->i_sector += i_read_blocks;
939
940     i_packet = 0;
941
942     /* Read headers to compute payload length */
943     for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
944     {
945         if( p_dvd->b_encrypted )
946         {
947             CSSDescrambleSector( p_dvd->p_css->pi_title_key, 
948                                  p_vec[i_iovec].iov_base );
949             ((u8*)(p_vec[i_iovec].iov_base))[0x14] &= 0x8F;
950         }
951
952         i_pos = 0;
953
954         while( i_pos < p_netlist->i_buffer_size )
955         {
956             pi_cur = (u8*)(p_vec[i_iovec].iov_base + i_pos);
957
958             /*default header */
959             if( U32_AT( pi_cur ) != 0x1BA )
960             {
961                 /* That's the case for all packets, except pack header. */
962                 i_packet_size = U16_AT( pi_cur + 4 );
963                 pp_packets[i_packet] = DVDNewPtr( p_netlist );
964             }
965             else
966             {
967                 /* Pack header. */
968                 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
969                 {
970                     /* MPEG-2 */
971                     i_packet_size = 8;
972                 }
973                 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
974                 {
975                     /* MPEG-1 */
976                     i_packet_size = 6;
977                 }
978                 else
979                 {
980                     intf_ErrMsg( "Unable to determine stream type" );
981                     return( -1 );
982                 }
983
984                 pp_packets[i_packet] = pp_data[i_iovec];
985
986             }
987
988             (*pp_data[i_iovec]->pi_refcount)++;
989
990             pp_packets[i_packet]->pi_refcount = pp_data[i_iovec]->pi_refcount;
991
992             pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
993
994             pp_packets[i_packet]->p_payload_start =
995                     pp_packets[i_packet]->p_buffer + i_pos;
996
997             pp_packets[i_packet]->p_payload_end =
998                     pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
999
1000             i_packet++;
1001             i_pos += i_packet_size + 6;
1002         }
1003     }
1004
1005     pp_packets[i_packet] = NULL;
1006
1007 //    vlc_mutex_lock( &p_input->stream.stream_lock );
1008
1009     p_input->stream.p_selected_area->i_tell +=
1010                                         p_dvd->i_block_once *DVD_LB_SIZE;
1011     b_eof = p_input->stream.p_selected_area->i_tell < p_dvd->i_size ? 0 : 1;
1012
1013 //    vlc_mutex_unlock( &p_input->stream.stream_lock );
1014
1015     if( ( i_read_blocks == p_dvd->i_block_once ) && ( !b_eof ) )
1016     {
1017         return 0;
1018     }
1019     else
1020     {
1021         return 1;
1022     }
1023 }
1024
1025
1026 /*****************************************************************************
1027  * DVDRewind : reads a stream backward
1028  *****************************************************************************/
1029 static int DVDRewind( input_thread_t * p_input )
1030 {
1031     return( -1 );
1032 }
1033
1034 /*****************************************************************************
1035  * DVDSeek : Goes to a given position on the stream ; this one is used by the 
1036  * input and translate chronological position from input to logical postion
1037  * on the device
1038  * ---
1039  * The lock should be taken before calling this function.
1040  *****************************************************************************/
1041 static void DVDSeek( input_thread_t * p_input, off_t i_off )
1042 {
1043     thread_dvd_data_t *     p_dvd;
1044     pgc_t *                 p_pgc;
1045     off_t                   i_pos;
1046     int                     i_prg_cell;
1047     int                     i_cell;
1048     int                     i_chapter;
1049     
1050     p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1051     p_pgc = &p_dvd->ifo.vts.pgci_ti.p_srp[p_dvd->i_vts_title-1].pgc;
1052
1053     /* update navigation data */
1054     p_dvd->i_sector = i_off >> 11;
1055
1056     i_prg_cell = 0;
1057     i_cell = 0;
1058     i_chapter = 1;
1059
1060     /* parse vobu address map to find program cell */
1061     while( p_pgc->p_cell_play_inf[i_prg_cell].i_lsector < p_dvd->i_sector  )
1062     {
1063         i_prg_cell++;
1064     }
1065     /* parse cell address map to find cell */
1066     while( p_dvd->ifo.vts.c_adt.p_cell_inf[i_cell].i_esector <=
1067            p_dvd->i_sector + 1 )
1068     {
1069         i_cell++;
1070     }
1071
1072     p_dvd->i_prg_cell = i_prg_cell;
1073     p_dvd->i_cell = i_cell - 1;             /* DVDFindSector will add one */
1074
1075     /* check coherence of data */
1076     DVDFindSector( p_dvd );
1077
1078     /* update chapter */
1079     while( p_pgc->prg_map.pi_entry_cell[i_chapter-1] - 1 < p_dvd->i_prg_cell )
1080     {
1081         i_chapter++;
1082     }
1083
1084     p_dvd->i_chapter = i_chapter;
1085
1086     /* we have to take care of offset of beginning of title */
1087     i_pos = i_off + p_input->stream.p_selected_area->i_start;
1088
1089     /* with DVD, we have to be on a sector boundary */
1090     i_pos = i_pos & (~0x7ff);
1091
1092     i_pos = lseek( p_dvd->i_fd, i_pos, SEEK_SET );
1093
1094     p_input->stream.p_selected_area->i_tell = i_pos -
1095                                     p_input->stream.p_selected_area->i_start;
1096     p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1097
1098     return;
1099 }