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