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