]> git.sesse.net Git - vlc/blob - plugins/dvd/input_dvd.c
43c8609c3c59f88c5227c26838665616baec1621
[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.35 2001/04/01 07:31:38 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  * DVDFindCell: adjust the title cell index with the program cell
354  *****************************************************************************/
355 static int DVDFindCell( thread_dvd_data_t * p_dvd )
356 {
357     int                 i_cell;
358     int                 i_index;
359
360 #define title \
361         p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_program_chain-1].title
362 #define cell  p_dvd->p_ifo->vts.cell_inf
363
364     i_cell = p_dvd->i_cell;
365     i_index = p_dvd->i_prg_cell;
366
367     while( ( ( title.p_cell_pos[i_index].i_vob_id !=
368                    cell.p_cell_map[i_cell].i_vob_id ) ||
369       ( title.p_cell_pos[i_index].i_cell_id !=
370                    cell.p_cell_map[i_cell].i_cell_id ) ) &&
371            ( i_cell < cell.i_cell_nb ) )
372     {
373         i_cell++;
374     }
375
376     if( i_cell == cell.i_cell_nb )
377     {
378         return -1;
379     }
380     else
381     {
382         p_dvd->i_cell = i_cell;
383         return 0;
384     }
385 #undef title
386 #undef cell
387 }
388
389 /*****************************************************************************
390  * DVDFindSector: find cell index in adress map from index in
391  * information table program map and give corresponding sectors.
392  *****************************************************************************/
393 static int DVDFindSector( thread_dvd_data_t * p_dvd )
394 {
395 #define title \
396         p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_program_chain-1].title
397
398     if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
399     {
400         p_dvd->i_prg_cell++;
401     }
402
403     if( DVDFindCell( p_dvd ) < 0 )
404     {
405         return -1;
406     }
407
408     /* Find start and end sectors of new cell */
409     p_dvd->i_sector = MAX(
410          p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
411          title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
412     p_dvd->i_end_sector = MIN(
413          p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
414          title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
415
416 //intf_WarnMsg( 3, "cell: %d sector1: 0x%x end1: 0x%x\nindex: %d sector2: 0x%x end2: 0x%x", p_dvd->i_cell, p_dvd->p_ifo->p_vts->c_adt.p_cell_inf[p_dvd->i_cell].i_ssector, p_dvd->p_ifo->p_vts->c_adt.p_cell_inf[p_dvd->i_cell].i_esector, p_dvd->i_prg_cell, p_pgc->p_cell_play_inf[p_dvd->i_prg_cell].i_entry_sector, p_pgc->p_cell_play_inf[p_dvd->i_prg_cell].i_lsector );
417 #undef title
418
419     return 0;
420 }
421
422 /*****************************************************************************
423  * DVDChapterSelect: find the cell corresponding to requested chapter
424  * When called to find chapter 1, also sets title size and end.
425  *****************************************************************************/
426 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
427 {
428 #define title \
429         p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_program_chain-1].title
430
431     /* Find cell index in Program chain for current chapter */
432     p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
433     p_dvd->i_cell = 0;
434
435     /* Search for cell_index in cell adress_table and initialize start sector */
436     DVDFindSector( p_dvd );
437
438     /* start is : beginning of vts vobs + offset to vob x */
439     p_dvd->i_start = p_dvd->i_title_start +
440                      DVD_LB_SIZE * (off_t)( p_dvd->i_sector );
441
442     /* Position the fd pointer on the right address */
443     p_dvd->i_start = lseek( p_dvd->i_fd, p_dvd->i_start, SEEK_SET );
444
445     p_dvd->i_chapter = i_chapter;
446 #undef title
447     return 0;
448 }
449
450 /*****************************************************************************
451  * DVDSetArea: initialize input data for title x, chapter y.
452  * It should be called for each user navigation request, and to change
453  * audio or sub-picture streams.
454  * ---
455  * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
456  * i_audio, i_spu start from 1 ; 0 means off.
457  * A negative value for an argument means it does not change
458  *****************************************************************************/
459 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
460 {
461     thread_dvd_data_t *  p_dvd;
462     es_descriptor_t *    p_es;
463     int                  i_audio;
464     int                  i_spu;
465     u16                  i_id;
466     u8                   i_ac3;
467     u8                   i_mpeg;
468     u8                   i_sub_pic;
469     u8                   i;
470     boolean_t            b_last;
471
472     p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
473
474     vlc_mutex_lock( &p_input->stream.stream_lock );
475
476     if( p_area != p_input->stream.p_selected_area )
477     {
478
479         /*
480          *  We have to load all title information
481          */
482         /* Change the default area */
483         p_input->stream.p_selected_area =
484                     p_input->stream.pp_areas[p_area->i_id];
485
486         /* title number: it is not vts nb! */
487         p_dvd->i_title = p_area->i_id;
488         p_dvd->p_ifo->i_title = p_dvd->i_title;
489
490         /* ifo vts */
491         IfoTitleSet( p_dvd->p_ifo );
492         intf_WarnMsg( 2, "ifo info: vts initialized" );
493
494 #define vmg p_dvd->p_ifo->vmg
495 #define vts p_dvd->p_ifo->vts
496         /* title position inside the selected vts */
497         p_dvd->i_vts_title =
498                     vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
499         p_dvd->i_program_chain =
500           vts.title_inf.p_title_start[p_dvd->i_vts_title-1].i_program_chain_num;
501
502         /* css title key for current vts */
503         if( p_dvd->b_encrypted )
504         {
505             p_dvd->p_css->i_title =
506                     vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_set_num;
507             p_dvd->p_css->i_title_pos =
508                     vts.i_pos +
509                     vts.manager_inf.i_title_vob_start_sector * DVD_LB_SIZE;
510             CSSGetKey( p_dvd->p_css );
511             intf_WarnMsg( 2, "css info: vts key initialized" );
512         }
513     
514         /*
515          * Set selected title start and size
516          */
517         
518         /* title set offset */
519         p_dvd->i_title_start = vts.i_pos + DVD_LB_SIZE *
520                       (off_t)( vts.manager_inf.i_title_vob_start_sector );
521
522         /* last video cell */
523         p_dvd->i_cell = 0;
524         p_dvd->i_prg_cell = -1 +
525             vts.title_unit.p_title[p_dvd->i_program_chain-1].title.i_cell_nb;
526         DVDFindCell( p_dvd );
527
528         /* temporary hack to fix size in some dvds */
529         if( p_dvd->i_cell >= vts.cell_inf.i_cell_nb )
530         {
531             p_dvd->i_cell = vts.cell_inf.i_cell_nb - 1;
532         }
533
534         p_dvd->i_sector = 0;
535         p_dvd->i_size = DVD_LB_SIZE *
536           (off_t)( vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
537
538         DVDChapterSelect( p_dvd, 1 );
539
540         p_dvd->i_size -= (off_t)( p_dvd->i_sector + 1 ) *DVD_LB_SIZE;
541
542         intf_WarnMsg( 2, "dvd info: title: %d", p_dvd->i_title );
543         intf_WarnMsg( 2, "dvd info: vobstart at: %lld", p_dvd->i_start );
544         intf_WarnMsg( 2, "dvd info: stream size: %lld", p_dvd->i_size );
545         intf_WarnMsg( 2, "dvd info: number of chapters: %d",
546                    vmg.title_inf.p_attr[p_dvd->i_title-1].i_chapter_nb );
547
548         /* Area definition */
549         p_input->stream.p_selected_area->i_start = p_dvd->i_start;
550         p_input->stream.p_selected_area->i_size = p_dvd->i_size;
551
552         /*
553          * Destroy obsolete ES by reinitializing program 0
554          * and find all ES in title with ifo data
555          */
556         if( p_input->stream.pp_programs != NULL )
557         {
558             /* We don't use input_EndStream here since
559              * we keep area structures */
560
561             for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
562             {
563                 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
564             }
565
566             input_DelProgram( p_input, p_input->stream.pp_programs[0] );
567
568             p_input->stream.pp_selected_es = NULL;
569             p_input->stream.i_selected_es_number = 0;
570         }
571
572         input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
573
574         p_es = NULL;
575
576         /* ES 0 -> video MPEG2 */
577         p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
578         p_es->i_stream_id = 0xe0;
579         p_es->i_type = MPEG2_VIDEO_ES;
580         input_SelectES( p_input, p_es );
581         intf_WarnMsg( 1, "dvd info: video MPEG2 stream" );
582
583         /* Audio ES, in the order they appear in .ifo */
584             
585         i_ac3 = 0x7f;
586         i_mpeg = 0xc0;
587
588         for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
589         {
590
591 #ifdef DEBUG
592         intf_DbgMsg( "Audio %d: %x %x %x %x %x %x %x %x %x %x %x %x\n", i,
593             vts.manager_inf.p_audio_attr[i-1].i_num_channels,
594             vts.manager_inf.p_audio_attr[i-1].i_coding_mode,
595             vts.manager_inf.p_audio_attr[i-1].i_multichannel_extension,
596             vts.manager_inf.p_audio_attr[i-1].i_type,
597             vts.manager_inf.p_audio_attr[i-1].i_appl_mode,
598             vts.manager_inf.p_audio_attr[i-1].i_foo,
599             vts.manager_inf.p_audio_attr[i-1].i_bar,
600             vts.manager_inf.p_audio_attr[i-1].i_appl_mode,
601             vts.manager_inf.p_audio_attr[i-1].i_quantization,
602             vts.manager_inf.p_audio_attr[i-1].i_sample_freq,
603             vts.manager_inf.p_audio_attr[i-1].i_lang_code,
604             vts.manager_inf.p_audio_attr[i-1].i_caption );
605 #endif
606
607             switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
608             {
609             case 0x00:              /* AC3 */
610                 i_id = ( ( i_ac3 + i ) << 8 ) | 0xbd;
611                 p_es = input_AddES( p_input,
612                                     p_input->stream.pp_programs[0], i_id, 0 );
613                 p_es->i_stream_id = 0xbd;
614                 p_es->i_type = AC3_AUDIO_ES;
615                 p_es->b_audio = 1;
616                 strcpy( p_es->psz_desc, Language( hton16(
617                     vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
618
619                 intf_WarnMsg( 1, "dvd info: audio stream %d %s\t(0x%x)",
620                               i, p_es->psz_desc, i_id );
621
622                 break;
623             case 0x02:
624             case 0x03:              /* MPEG audio */
625                 i_id = 0xbf + i;
626                 p_es = input_AddES( p_input,
627                                     p_input->stream.pp_programs[0], i_id, 0 );
628                 p_es->i_stream_id = i_id;
629                 p_es->i_type = MPEG2_AUDIO_ES;
630                 p_es->b_audio = 1;
631                 strcpy( p_es->psz_desc, Language( hton16(
632                     vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
633
634                 intf_WarnMsg( 1, "dvd info: audio stream %d %s\t(0x%x)",
635                               i, p_es->psz_desc, i_id );
636
637                 break;
638             case 0x04:              /* LPCM */
639                 i_id = 0;
640                 intf_ErrMsg( "dvd warning: LPCM audio not handled yet" );
641                 break;
642             case 0x06:              /* DTS */
643                 i_id = 0;
644                 i_ac3--;
645                 intf_ErrMsg( "dvd warning: DTS audio not handled yet" );
646                 break;
647             default:
648                 i_id = 0;
649                 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
650                          vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
651             }
652         
653         }
654     
655         /* Sub Picture ES */
656            
657         b_last = 0;
658         i_sub_pic = 0x20;
659         for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
660         {
661             if( !b_last )
662             {
663                 i_id = ( i_sub_pic++ << 8 ) | 0xbd;
664                 p_es = input_AddES( p_input,
665                                     p_input->stream.pp_programs[0], i_id, 0 );
666                 p_es->i_stream_id = 0xbd;
667                 p_es->i_type = DVD_SPU_ES;
668                 p_es->b_spu = 1;
669                 strcpy( p_es->psz_desc, Language( hton16(
670                     vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) ); 
671                 intf_WarnMsg( 1, "dvd info: spu stream %d %s\t(0x%x)",
672                               i, p_es->psz_desc, i_id );
673     
674                 /* The before the last spu has a 0x0 prefix */
675                 b_last =
676                     ( vts.manager_inf.p_spu_attr[i].i_prefix == 0 ); 
677             }
678         }
679
680         /* For audio: first one if none or a not existing one specified */
681         i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
682         if( i_audio < 0 || i_audio > vts.manager_inf.i_audio_nb )
683         {
684             main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
685             i_audio = 1;
686         }
687         if( i_audio > 0 )
688         {
689             input_SelectES( p_input, p_input->stream.pp_es[i_audio] );
690         }
691     
692         /* for spu, default is none */
693         i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
694         if( i_spu < 0 || i_spu > vts.manager_inf.i_spu_nb )
695         {
696             main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
697             i_spu = 0;
698         }
699         if( i_spu > 0 )
700         {
701             i_spu += vts.manager_inf.i_audio_nb;
702             input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
703         }
704     } // i_title >= 0
705     else
706     {
707         p_area = p_input->stream.p_selected_area;
708     }
709
710     /*
711      * Chapter selection
712      */
713
714     if( ( p_area->i_part > 0 ) &&
715         ( p_area->i_part <= p_area->i_part_nb ) )
716     {
717         DVDChapterSelect( p_dvd, p_area->i_part );
718
719         p_input->stream.p_selected_area->i_tell = p_dvd->i_start -
720                                                   p_area->i_start;
721         p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
722
723         intf_WarnMsg( 2, "dvd info: chapter %d start at: %lld",
724                                     p_area->i_part, p_area->i_tell );
725     }
726
727     /* No PSM to read in DVD mode, we already have all information */
728     p_input->stream.pp_programs[0]->b_is_ok = 1;
729     p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_START;
730
731     vlc_mutex_unlock( &p_input->stream.stream_lock );
732 #undef vts
733 #undef vmg
734     return 0;
735 }
736
737 /*****************************************************************************
738  * DVDInit: initializes DVD structures
739  *****************************************************************************/
740 static void DVDInit( input_thread_t * p_input )
741 {
742     thread_dvd_data_t *  p_dvd;
743     int                  i_title;
744     int                  i_chapter;
745     int                  i;
746
747     if( (p_dvd = malloc( sizeof(thread_dvd_data_t) )) == NULL )
748     {
749         intf_ErrMsg( "Out of memory" );
750         p_input->b_error = 1;
751         return;
752     }
753
754     p_input->p_plugin_data = (void *)p_dvd;
755     p_input->p_method_data = NULL;
756
757     p_dvd->i_fd = p_input->i_handle;
758
759     p_dvd->i_block_once = 32;
760     p_input->i_read_once = 128;
761
762     p_dvd->b_encrypted = DVDCheckCSS( p_input );
763
764     lseek( p_input->i_handle, 0, SEEK_SET );
765
766     /* Reading structures initialisation */
767     p_input->p_method_data =
768         DVDNetlistInit( 8192, 16384, 2048, DVD_LB_SIZE, p_dvd->i_block_once );
769     intf_WarnMsg( 2, "dvd info: netlist initialized" );
770
771     /* Ifo initialisation */
772     if( IfoInit( &p_dvd->p_ifo, p_input->i_handle ) < 0 )
773     {
774             intf_ErrMsg( "ifo error: fatal failure" );
775             free( p_dvd );
776             p_input->b_error = 1;
777             return;
778     }
779
780     /* CSS initialisation */
781     if( p_dvd->b_encrypted )
782     {
783         p_dvd->p_css = CSSInit( p_input->i_handle );
784
785         if( p_dvd->p_css == NULL )
786         {
787             intf_ErrMsg( "css error: fatal failure" );
788             free( p_dvd );
789             p_input->b_error = 1;
790             return;
791         }
792
793         intf_WarnMsg( 2, "css info: initialized" );
794     }
795
796     /* Initialize ES structures */
797     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
798
799     /* Set stream and area data */
800     vlc_mutex_lock( &p_input->stream.stream_lock );
801
802 #define title_inf p_dvd->p_ifo->vmg.title_inf
803     intf_WarnMsg( 2, "dvd info: number of titles: %d", title_inf.i_title_nb );
804
805 #define area p_input->stream.pp_areas
806     /* We start from 1 here since area 0 is reserved for video_ts.vob */
807     for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
808     {
809         input_AddArea( p_input );
810
811         /* Titles are Program Chains */
812         area[i]->i_id = i;
813
814         /* Absolute start offset and size 
815          * We can only set that with vts ifo, so we do it during the
816          * first call to DVDSetArea */
817         area[i]->i_start = 0;
818         area[i]->i_size = 0;
819
820         /* Number of chapters */
821         area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
822         area[i]->i_part = 1;
823
824         /* Offset to vts_i_0.ifo */
825         area[i]->i_plugin_data = p_dvd->p_ifo->i_off +
826                        ( title_inf.p_attr[i-1].i_start_sector * DVD_LB_SIZE );
827     }   
828 #undef area
829
830     vlc_mutex_unlock( &p_input->stream.stream_lock );
831
832     /* Get requested title - if none try the first title */
833     i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
834     if( i_title <= 0 || i_title > title_inf.i_title_nb )
835     {
836         i_title = 1;
837     }
838 #undef title_inf
839     /* Get requested chapter - if none defaults to first one */
840     i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
841     if( i_chapter <= 0 )
842     {
843         i_chapter = 1;
844     }
845
846     p_input->stream.pp_areas[i_title]->i_part = i_chapter;
847
848     /* set title, chapter, audio and subpic */
849     DVDSetArea( p_input, p_input->stream.pp_areas[i_title] );
850
851     return;
852 }
853
854 /*****************************************************************************
855  * DVDEnd: frees unused data
856  *****************************************************************************/
857 static void DVDEnd( input_thread_t * p_input )
858 {
859     thread_dvd_data_t *     p_dvd;
860     dvd_netlist_t *         p_netlist;
861
862     p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
863     p_netlist = (dvd_netlist_t *)p_input->p_method_data;
864
865     if( p_dvd->b_encrypted )
866     {
867         CSSEnd( p_dvd->p_css );
868     }
869
870     IfoEnd( p_dvd->p_ifo );
871     free( p_dvd );
872     DVDNetlistEnd( p_netlist );
873 }
874
875 /*****************************************************************************
876  * DVDRead: reads data packets into the netlist.
877  *****************************************************************************
878  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
879  * EOF.
880  *****************************************************************************/
881 static int DVDRead( input_thread_t * p_input,
882                     data_packet_t ** pp_packets )
883 {
884     thread_dvd_data_t *     p_dvd;
885     dvd_netlist_t *         p_netlist;
886     struct iovec *          p_vec;
887     struct data_packet_s *  pp_data[p_input->i_read_once];
888     u8 *                    pi_cur;
889     int                     i_block_once;
890     int                     i_packet_size;
891     int                     i_iovec;
892     int                     i_packet;
893     int                     i_pos;
894     int                     i_read_bytes;
895     int                     i_read_blocks;
896     off_t                   i_off;
897     boolean_t               b_eof;
898
899     p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
900     p_netlist = (dvd_netlist_t *)p_input->p_method_data;
901 #define title \
902     p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_program_chain-1].title
903
904     /* Get an iovec pointer */
905     if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
906     {
907         intf_ErrMsg( "DVD: read error" );
908         return -1;
909     }
910
911     i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
912
913     /* Get the position of the next cell if we're at cell end */
914     if( i_block_once <= 0 )
915     {
916         p_dvd->i_cell++;
917
918         /* Find cell index in adress map */
919         if( DVDFindSector( p_dvd ) < 0 )
920         {
921             pp_packets[0] = NULL;
922             intf_ErrMsg( "dvd error: can't find next cell" );
923             return 1;
924         }
925
926         /* Position the fd pointer on the right address */
927         i_off = lseek( p_dvd->i_fd,
928                        p_dvd->i_title_start +
929                        (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET );
930
931         /* update chapter : it will be easier when we have navigation
932          * ES support */
933         if( title.chapter_map.pi_start_cell[p_dvd->i_chapter-1] <=
934             p_dvd->i_prg_cell )
935         {
936             p_dvd->i_chapter++;
937         }
938
939         vlc_mutex_lock( &p_input->stream.stream_lock );
940
941         p_input->stream.p_selected_area->i_tell = i_off -
942                                     p_input->stream.p_selected_area->i_start;
943         p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
944
945         /* the synchro has to be reinitialized when we change cell */
946         p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_START;
947
948         vlc_mutex_unlock( &p_input->stream.stream_lock );
949
950         i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
951     }
952
953     /* the number of blocks read is the maw between the requested
954      * value and the leaving block in the cell */
955     if( i_block_once > p_dvd->i_block_once )
956     {
957         i_block_once = p_dvd->i_block_once;
958     }
959 //intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
960
961     p_netlist->i_read_once = i_block_once;
962
963     /* Reads from DVD */
964     i_read_bytes = readv( p_dvd->i_fd, p_vec, i_block_once );
965     i_read_blocks = ( i_read_bytes + 0x7ff ) >> 11;
966
967     /* Update netlist indexes */
968     DVDMviovec( p_netlist, i_read_blocks, pp_data );
969
970     /* Update global position */
971     p_dvd->i_sector += i_read_blocks;
972
973     i_packet = 0;
974
975     /* Read headers to compute payload length */
976     for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
977     {
978         if( p_dvd->b_encrypted )
979         {
980             CSSDescrambleSector( p_dvd->p_css->pi_title_key, 
981                                  p_vec[i_iovec].iov_base );
982             ((u8*)(p_vec[i_iovec].iov_base))[0x14] &= 0x8F;
983         }
984
985         i_pos = 0;
986
987         while( i_pos < p_netlist->i_buffer_size )
988         {
989             pi_cur = (u8*)(p_vec[i_iovec].iov_base + i_pos);
990
991             /*default header */
992             if( U32_AT( pi_cur ) != 0x1BA )
993             {
994                 /* That's the case for all packets, except pack header. */
995                 i_packet_size = U16_AT( pi_cur + 4 );
996                 pp_packets[i_packet] = DVDNewPtr( p_netlist );
997             }
998             else
999             {
1000                 /* Pack header. */
1001                 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
1002                 {
1003                     /* MPEG-2 */
1004                     i_packet_size = 8;
1005                 }
1006                 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
1007                 {
1008                     /* MPEG-1 */
1009                     i_packet_size = 6;
1010                 }
1011                 else
1012                 {
1013                     intf_ErrMsg( "Unable to determine stream type" );
1014                     return( -1 );
1015                 }
1016
1017                 pp_packets[i_packet] = pp_data[i_iovec];
1018
1019             }
1020
1021             (*pp_data[i_iovec]->pi_refcount)++;
1022
1023             pp_packets[i_packet]->pi_refcount = pp_data[i_iovec]->pi_refcount;
1024
1025             pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
1026
1027             pp_packets[i_packet]->p_payload_start =
1028                     pp_packets[i_packet]->p_buffer + i_pos;
1029
1030             pp_packets[i_packet]->p_payload_end =
1031                     pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
1032
1033             pp_packets[i_packet]->p_next = NULL;
1034             pp_packets[i_packet]->b_discard_payload = 0;
1035
1036             i_packet++;
1037             i_pos += i_packet_size + 6;
1038         }
1039     }
1040
1041     pp_packets[i_packet] = NULL;
1042
1043     vlc_mutex_lock( &p_input->stream.stream_lock );
1044
1045     p_input->stream.p_selected_area->i_tell += i_read_bytes;
1046     b_eof = p_input->stream.p_selected_area->i_tell < p_dvd->i_size ? 0 : 1;
1047
1048     vlc_mutex_unlock( &p_input->stream.stream_lock );
1049
1050     if( ( i_read_blocks == i_block_once ) && ( !b_eof ) )
1051     {
1052         return 0;
1053     }
1054     else
1055     {
1056         return 1;
1057     }
1058 #undef title
1059 }
1060
1061
1062 /*****************************************************************************
1063  * DVDRewind : reads a stream backward
1064  *****************************************************************************/
1065 static int DVDRewind( input_thread_t * p_input )
1066 {
1067     return( -1 );
1068 }
1069
1070 /*****************************************************************************
1071  * DVDSeek : Goes to a given position on the stream ; this one is used by the 
1072  * input and translate chronological position from input to logical postion
1073  * on the device
1074  * ---
1075  * The lock should be taken before calling this function.
1076  *****************************************************************************/
1077 static void DVDSeek( input_thread_t * p_input, off_t i_off )
1078 {
1079     thread_dvd_data_t *     p_dvd;
1080     off_t                   i_pos;
1081     int                     i_prg_cell;
1082     int                     i_cell;
1083     int                     i_chapter;
1084     
1085     p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1086 #define title \
1087     p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_program_chain-1].title
1088
1089     /* we have to take care of offset of beginning of title */
1090     i_pos = i_off + p_input->stream.p_selected_area->i_start
1091                   - p_dvd->i_title_start;
1092
1093     /* update navigation data */
1094     p_dvd->i_sector = i_pos >> 11;
1095
1096     i_prg_cell = 0;
1097     i_chapter = 1;
1098
1099     /* parse vobu address map to find program cell */
1100     while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector  )
1101     {
1102         i_prg_cell++;
1103     }
1104
1105     p_dvd->i_prg_cell = i_prg_cell;
1106     p_dvd->i_cell = 0;
1107
1108     /* Find first title cell which is inside program cell */
1109     DVDFindCell( p_dvd );
1110
1111     i_cell = p_dvd->i_cell;
1112 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1113     /* parse cell address map to find title cell containing sector */
1114     while( cell.i_end_sector < p_dvd->i_sector )
1115     {
1116         i_cell++;
1117     }
1118
1119     p_dvd->i_cell = i_cell;
1120
1121     p_dvd->i_end_sector = MIN(
1122             cell.i_end_sector,
1123             title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1124
1125     /* update chapter */
1126     while( title.chapter_map.pi_start_cell[i_chapter-1] <= p_dvd->i_prg_cell )
1127     {
1128         i_chapter++;
1129     }
1130
1131     p_dvd->i_chapter = i_chapter;
1132     p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1133
1134     p_input->stream.p_selected_area->i_tell =
1135                 lseek( p_dvd->i_fd, p_dvd->i_title_start +
1136                        (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET ) -
1137                 p_input->stream.p_selected_area->i_start;
1138
1139     intf_WarnMsg( 1, "Program Cell: %d Cell: %d Chapter: %d",
1140                      i_prg_cell, i_cell, p_dvd->i_chapter );
1141
1142     return;
1143 }