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