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