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