]> git.sesse.net Git - vlc/blob - plugins/dvd/input_dvd.c
* Added support for some terribly braindead DVD subtitles in Kenshin
[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.57 2001/05/07 04:42:42 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 /*****************************************************************************
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 #ifdef STRNCASECMP_IN_STRINGS_H
55 #   include <strings.h>
56 #endif
57 #include <errno.h>
58
59 #include "config.h"
60 #include "common.h"
61 #include "threads.h"
62 #include "mtime.h"
63 #include "tests.h"
64
65 #include "intf_msg.h"
66
67 #include "main.h"
68
69 #include "stream_control.h"
70 #include "input_ext-intf.h"
71 #include "input_ext-dec.h"
72
73 #include "input.h"
74
75 #include "dvd_netlist.h"
76 #include "dvd_ifo.h"
77 #include "dvd_css.h"
78 #include "input_dvd.h"
79 #include "mpeg_system.h"
80
81 #include "debug.h"
82
83 #include "modules.h"
84
85 /*****************************************************************************
86  * Local tables
87  *****************************************************************************/
88 static struct
89 {
90     char    p_code[3];
91     char    p_lang_long[20];
92 }
93
94 lang_tbl[] =
95 {
96     /* The ISO 639 language codes.
97      * Language names with * prefix are not spelled in their own language 
98      */
99     { "  ", "Not Specified" },
100     { "aa", "*Afar" },
101     { "ab", "*Abkhazian" },
102     { "af", "*Afrikaans" },
103     { "am", "*Amharic" },
104     { "ar", "*Arabic" },
105     { "as", "*Assamese" },
106     { "ay", "*Aymara" },
107     { "az", "*Azerbaijani" },
108     { "ba", "*Bashkir" },
109     { "be", "*Byelorussian" },
110     { "bg", "*Bulgarian" },
111     { "bh", "*Bihari" },
112     { "bi", "*Bislama" },
113     { "bn", "*Bengali; Bangla" },
114     { "bo", "*Tibetan" },
115     { "br", "*Breton" },
116     { "ca", "*Catalan" },
117     { "co", "*Corsican" },
118     { "cs", "*Czech(Ceske)" },
119     { "cy", "*Welsh" },
120     { "da", "Dansk" },
121     { "de", "Deutsch" },
122     { "dz", "*Bhutani" },
123     { "el", "*Greek" },
124     { "en", "English" },
125     { "eo", "*Esperanto" },
126     { "es", "Espanol" },
127     { "et", "*Estonian" },
128     { "eu", "*Basque" },
129     { "fa", "*Persian" },
130     { "fi", "Suomi" },
131     { "fj", "*Fiji" },
132     { "fo", "*Faroese" },
133     { "fr", "Francais" },
134     { "fy", "*Frisian" },
135     { "ga", "*Irish" },
136     { "gd", "*Scots Gaelic" },
137     { "gl", "*Galician" },
138     { "gn", "*Guarani" },
139     { "gu", "*Gujarati" },
140     { "ha", "*Hausa" },
141     { "he", "*Hebrew" },                                      /* formerly iw */
142     { "hi", "*Hindi" },
143     { "hr", "Hrvatski" },                                        /* Croatian */
144     { "hu", "Magyar" },
145     { "hy", "*Armenian" },
146     { "ia", "*Interlingua" },
147     { "id", "*Indonesian" },                                  /* formerly in */
148     { "ie", "*Interlingue" },
149     { "ik", "*Inupiak" },
150     { "in", "*Indonesian" },                               /* replaced by id */
151     { "is", "Islenska" },
152     { "it", "Italiano" },
153     { "iu", "*Inuktitut" },
154     { "iw", "*Hebrew" },                                   /* replaced by he */
155     { "ja", "*Japanese" },
156     { "ji", "*Yiddish" },                                  /* replaced by yi */
157     { "jw", "*Javanese" },
158     { "ka", "*Georgian" },
159     { "kk", "*Kazakh" },
160     { "kl", "*Greenlandic" },
161     { "km", "*Cambodian" },
162     { "kn", "*Kannada" },
163     { "ko", "*Korean" },
164     { "ks", "*Kashmiri" },
165     { "ku", "*Kurdish" },
166     { "ky", "*Kirghiz" },
167     { "la", "*Latin" },
168     { "ln", "*Lingala" },
169     { "lo", "*Laothian" },
170     { "lt", "*Lithuanian" },
171     { "lv", "*Latvian, Lettish" },
172     { "mg", "*Malagasy" },
173     { "mi", "*Maori" },
174     { "mk", "*Macedonian" },
175     { "ml", "*Malayalam" },
176     { "mn", "*Mongolian" },
177     { "mo", "*Moldavian" },
178     { "mr", "*Marathi" },
179     { "ms", "*Malay" },
180     { "mt", "*Maltese" },
181     { "my", "*Burmese" },
182     { "na", "*Nauru" },
183     { "ne", "*Nepali" },
184     { "nl", "Nederlands" },
185     { "no", "Norsk" },
186     { "oc", "*Occitan" },
187     { "om", "*(Afan) Oromo" },
188     { "or", "*Oriya" },
189     { "pa", "*Punjabi" },
190     { "pl", "*Polish" },
191     { "ps", "*Pashto, Pushto" },
192     { "pt", "Portugues" },
193     { "qu", "*Quechua" },
194     { "rm", "*Rhaeto-Romance" },
195     { "rn", "*Kirundi" },
196     { "ro", "*Romanian"  },
197     { "ru", "*Russian" },
198     { "rw", "*Kinyarwanda" },
199     { "sa", "*Sanskrit" },
200     { "sd", "*Sindhi" },
201     { "sg", "*Sangho" },
202     { "sh", "*Serbo-Croatian" },
203     { "si", "*Sinhalese" },
204     { "sk", "*Slovak" },
205     { "sl", "*Slovenian" },
206     { "sm", "*Samoan" },
207     { "sn", "*Shona"  },
208     { "so", "*Somali" },
209     { "sq", "*Albanian" },
210     { "sr", "*Serbian" },
211     { "ss", "*Siswati" },
212     { "st", "*Sesotho" },
213     { "su", "*Sundanese" },
214     { "sv", "Svenska" },
215     { "sw", "*Swahili" },
216     { "ta", "*Tamil" },
217     { "te", "*Telugu" },
218     { "tg", "*Tajik" },
219     { "th", "*Thai" },
220     { "ti", "*Tigrinya" },
221     { "tk", "*Turkmen" },
222     { "tl", "*Tagalog" },
223     { "tn", "*Setswana" },
224     { "to", "*Tonga" },
225     { "tr", "*Turkish" },
226     { "ts", "*Tsonga" },
227     { "tt", "*Tatar" },
228     { "tw", "*Twi" },
229     { "ug", "*Uighur" },
230     { "uk", "*Ukrainian" },
231     { "ur", "*Urdu" },
232     { "uz", "*Uzbek" },
233     { "vi", "*Vietnamese" },
234     { "vo", "*Volapuk" },
235     { "wo", "*Wolof" },
236     { "xh", "*Xhosa" },
237     { "yi", "*Yiddish" },                                     /* formerly ji */
238     { "yo", "*Yoruba" },
239     { "za", "*Zhuang" },
240     { "zh", "*Chinese" },
241     { "zu", "*Zulu" },
242     { "\0", "" }
243 };
244
245 /*****************************************************************************
246  * Local prototypes
247  *****************************************************************************/
248 /* called from outside */
249 static int  DVDProbe    ( probedata_t *p_data );
250 static void DVDInit     ( struct input_thread_s * );
251 static void DVDEnd      ( struct input_thread_s * );
252 static int  DVDSetArea  ( struct input_thread_s *, struct input_area_s * );
253 static int  DVDRead     ( struct input_thread_s *, data_packet_t ** );
254 static void DVDSeek     ( struct input_thread_s *, off_t );
255 static int  DVDRewind   ( struct input_thread_s * );
256
257 /* called only inside */
258 static char * Language( u16 );
259 static int  DVDChooseAngle( thread_dvd_data_t * );
260 static int  DVDFindCell( thread_dvd_data_t * );
261 static int  DVDFindSector( thread_dvd_data_t * );
262 static int  DVDChapterSelect( thread_dvd_data_t *, int );
263
264 /*****************************************************************************
265  * Functions exported as capabilities. They are declared as static so that
266  * we don't pollute the namespace too much.
267  *****************************************************************************/
268 void _M( input_getfunctions )( function_list_t * p_function_list )
269 {
270 #define input p_function_list->functions.input
271     p_function_list->pf_probe = DVDProbe;
272     input.pf_init             = DVDInit;
273     input.pf_open             = input_FileOpen;
274     input.pf_close            = input_FileClose;
275     input.pf_end              = DVDEnd;
276     input.pf_read             = DVDRead;
277     input.pf_set_area         = DVDSetArea;
278     input.pf_demux            = input_DemuxPS;
279     input.pf_new_packet       = DVDNewPacket;
280     input.pf_new_pes          = DVDNewPES;
281     input.pf_delete_packet    = DVDDeletePacket;
282     input.pf_delete_pes       = DVDDeletePES;
283     input.pf_rewind           = DVDRewind;
284     input.pf_seek             = DVDSeek;
285 #undef input
286 }
287
288 /*
289  * Local tools to decode some data in ifo
290  */
291
292 /*****************************************************************************
293  * Language: gives the long language name from the two-letters ISO-639 code
294  *****************************************************************************/
295 static char * Language( u16 i_code )
296 {
297     int     i = 0;
298
299     while( memcmp( lang_tbl[i].p_code, &i_code, 2 ) &&
300            lang_tbl[i].p_lang_long[0] )
301     {
302         i++;
303     }
304
305     return lang_tbl[i].p_lang_long;
306 }
307
308 /*
309  * Data reading functions
310  */
311
312 /*****************************************************************************
313  * DVDProbe: verifies that the stream is a PS stream
314  *****************************************************************************/
315 static int DVDProbe( probedata_t *p_data )
316 {
317     input_thread_t * p_input = (input_thread_t *)p_data;
318
319     char * psz_name = p_input->p_source;
320     int i_handle;
321     int i_score = 5;
322
323     if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
324     {
325 #ifdef HAVE_CSS
326         return( 999 );
327 #else /* HAVE_CSS */
328         return( 998 );
329 #endif /* HAVE_CSS */
330     }
331
332     if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
333     {
334         /* If the user specified "dvd:" then it's probably a DVD */
335 #ifdef HAVE_CSS
336         i_score = 100;
337 #else /* HAVE_CSS */
338         i_score = 90;
339 #endif /* HAVE_CSS */
340         psz_name += 4;
341     }
342
343     i_handle = open( psz_name, 0 );
344     if( i_handle == -1 )
345     {
346         return( 0 );
347     }
348     close( i_handle );
349
350     return( i_score );
351 }
352
353 /*****************************************************************************
354  * DVDInit: initializes DVD structures
355  *****************************************************************************/
356 static void DVDInit( input_thread_t * p_input )
357 {
358     thread_dvd_data_t *  p_dvd;
359     input_area_t *       p_area;
360     int                  i_title;
361     int                  i_chapter;
362     int                  i;
363
364     /* I don't want DVDs to start playing immediately */
365 //    p_input->stream.i_new_status = PAUSE_S;
366
367     p_dvd = malloc( sizeof(thread_dvd_data_t) );
368     if( p_dvd == NULL )
369     {
370         intf_ErrMsg( "dvd error: out of memory" );
371         p_input->b_error = 1;
372         return;
373     }
374
375     p_input->p_plugin_data = (void *)p_dvd;
376     p_input->p_method_data = NULL;
377
378     p_dvd->i_fd = p_input->i_handle;
379
380     /* reading several block once seems to cause lock-up
381      * when using input_ToggleES
382      * who wrote thez damn buggy piece of shit ??? --stef */
383     p_dvd->i_block_once = 1;//32;
384     p_input->i_read_once = 8;//128;
385
386     i = CSSTest( p_input->i_handle );
387
388     if( i < 0 )
389     {
390         free( p_dvd );
391         p_input->b_error = 1;
392         return;
393     }
394
395     p_dvd->b_encrypted = i;
396
397     lseek( p_input->i_handle, 0, SEEK_SET );
398
399     /* Reading structures initialisation */
400     p_input->p_method_data =
401         DVDNetlistInit( 2048, 4096, 2048, DVD_LB_SIZE, p_dvd->i_block_once );
402     intf_WarnMsg( 2, "dvd info: netlist initialized" );
403
404     /* Ifo allocation & initialisation */
405     if( IfoCreate( p_dvd ) < 0 )
406     {
407         intf_ErrMsg( "dvd error: allcation error in ifo" );
408         p_input->b_error = 1;
409         return;
410     }
411
412     if( IfoInit( p_dvd->p_ifo ) < 0 )
413     {
414         intf_ErrMsg( "dvd error: fatal failure in ifo" );
415         free( p_dvd );
416         p_input->b_error = 1;
417         return;
418     }
419
420     /* CSS initialisation */
421     if( p_dvd->b_encrypted )
422     {
423         p_dvd->p_css = malloc( sizeof(css_t) );
424         if( p_dvd->p_css == NULL )
425         {
426             intf_ErrMsg( "dvd error: couldn't create css structure" );
427             free( p_dvd );
428             p_input->b_error = 1;
429             return;
430         }
431
432         p_dvd->p_css->i_agid = 0;
433
434         if( CSSInit( p_input->i_handle, p_dvd->p_css ) < 0 )
435         {
436             intf_ErrMsg( "dvd error: fatal failure in css" );
437             free( p_dvd->p_css );
438             free( p_dvd );
439             p_input->b_error = 1;
440             return;
441         }
442
443         intf_WarnMsg( 2, "dvd info: css initialized" );
444     }
445
446     /* Set stream and area data */
447     vlc_mutex_lock( &p_input->stream.stream_lock );
448
449     /* Initialize ES structures */
450     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
451
452     /* disc input method */
453     p_input->stream.i_method = INPUT_METHOD_DVD;
454
455 #define title_inf p_dvd->p_ifo->vmg.title_inf
456     intf_WarnMsg( 2, "dvd info: number of titles: %d", title_inf.i_title_nb );
457
458 #define area p_input->stream.pp_areas
459     /* We start from 1 here since the default area 0
460      * is reserved for video_ts.vob */
461     for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
462     {
463         input_AddArea( p_input );
464
465         /* Titles are Program Chains */
466         area[i]->i_id = i;
467
468         /* Absolute start offset and size 
469          * We can only set that with vts ifo, so we do it during the
470          * first call to DVDSetArea */
471         area[i]->i_start = 0;
472         area[i]->i_size = 0;
473
474         /* Number of chapters */
475         area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
476         area[i]->i_part = 1;
477
478         /* Number of angles */
479         area[i]->i_angle_nb = 0;
480         area[i]->i_angle = 1;
481
482         /* Offset to vts_i_0.ifo */
483         area[i]->i_plugin_data = p_dvd->p_ifo->i_off +
484                        ( title_inf.p_attr[i-1].i_start_sector * DVD_LB_SIZE );
485     }   
486 #undef area
487
488     /* Get requested title - if none try the first title */
489     i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
490     if( i_title <= 0 || i_title > title_inf.i_title_nb )
491     {
492         i_title = 1;
493     }
494
495 #undef title_inf
496
497     /* Get requested chapter - if none defaults to first one */
498     i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
499     if( i_chapter <= 0 )
500     {
501         i_chapter = 1;
502     }
503
504     p_input->stream.pp_areas[i_title]->i_part = i_chapter;
505
506     p_area = p_input->stream.pp_areas[i_title];
507
508     vlc_mutex_unlock( &p_input->stream.stream_lock );
509
510     /* set title, chapter, audio and subpic */
511     DVDSetArea( p_input, p_area );
512
513     return;
514 }
515
516 /*****************************************************************************
517  * DVDEnd: frees unused data
518  *****************************************************************************/
519 static void DVDEnd( input_thread_t * p_input )
520 {
521     thread_dvd_data_t *     p_dvd;
522     dvd_netlist_t *         p_netlist;
523
524     p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
525     p_netlist = (dvd_netlist_t *)p_input->p_method_data;
526
527     if( p_dvd->b_encrypted )
528     {
529         free( p_dvd->p_css );
530     }
531
532     IfoDestroy( p_dvd->p_ifo );
533     free( p_dvd );
534     DVDNetlistEnd( p_netlist );
535 }
536
537 /*****************************************************************************
538  * DVDSetArea: initialize input data for title x, chapter y.
539  * It should be called for each user navigation request, and to change
540  * audio or sub-picture streams.
541  * ---
542  * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
543  * i_audio, i_spu start from 1 ; 0 means off.
544  * A negative value for an argument means it does not change
545  *****************************************************************************/
546 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
547 {
548     thread_dvd_data_t *  p_dvd;
549     es_descriptor_t *    p_es;
550     int                  i_audio;
551     int                  i_spu;
552     u16                  i_id;
553     u8                   i_ac3;
554     u8                   i_mpeg;
555     u8                   i_lpcm;
556     u8                   i_sub_pic;
557     u8                   i;
558     int                  j;
559     boolean_t            b_last;
560
561     p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
562
563     vlc_mutex_lock( &p_input->stream.stream_lock );
564
565     if( p_area != p_input->stream.p_selected_area )
566     {
567
568         /*
569          *  We have to load all title information
570          */
571         /* Change the default area */
572         p_input->stream.p_selected_area =
573                     p_input->stream.pp_areas[p_area->i_id];
574
575 //        vlc_mutex_unlock( &p_input->stream.stream_lock );
576
577         /* title number: it is not vts nb!,
578          * it is what appears in the interface list */
579         p_dvd->i_title = p_area->i_id;
580         p_dvd->p_ifo->i_title = p_dvd->i_title;
581
582         /* uodate title environnement variable so that we don't
583          * loop on the same title forever */
584         main_PutIntVariable( INPUT_TITLE_VAR, p_dvd->i_title + 1 );
585
586         /* ifo vts */
587         if( IfoTitleSet( p_dvd->p_ifo ) < 0 )
588         {
589             intf_ErrMsg( "dvd error: fatal error in vts ifo" );
590             free( p_dvd );
591             p_input->b_error = 1;
592             return -1;
593         }
594
595 #define vmg p_dvd->p_ifo->vmg
596 #define vts p_dvd->p_ifo->vts
597         /* title position inside the selected vts */
598         p_dvd->i_vts_title =
599                     vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
600         p_dvd->i_title_id =
601           vts.title_inf.p_title_start[p_dvd->i_vts_title-1].i_title_id;
602
603         intf_WarnMsg( 3, "dvd: title %d vts_title %d pgc %d",
604                         p_dvd->i_title,
605                         p_dvd->i_vts_title,
606                         p_dvd->i_title_id );
607
608         /* css title key for current vts */
609         if( p_dvd->b_encrypted )
610         {
611             /* this one is vts number */
612             p_dvd->p_css->i_title =
613                     vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_set_num;
614             p_dvd->p_css->i_title_pos =
615                     vts.i_pos +
616                     vts.manager_inf.i_title_vob_start_sector * DVD_LB_SIZE;
617
618             j = CSSGetKey( p_input->i_handle, p_dvd->p_css );
619             if( j < 0 )
620             {
621                 intf_ErrMsg( "dvd error: fatal error in vts css key" );
622                 free( p_dvd );
623                 p_input->b_error = 1;
624                 return -1;
625             }
626             else if( j > 0 )
627             {
628                 intf_ErrMsg( "dvd error: css decryption unavailable" );
629                 free( p_dvd );
630                 p_input->b_error = 1;
631                 return -1;
632             }
633         }
634
635         /*
636          * Angle management
637          */
638         p_dvd->i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
639         p_dvd->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
640         if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
641         {
642             p_dvd->i_angle = 1;
643         }
644     
645         /*
646          * Set selected title start and size
647          */
648         
649         /* title set offset */
650         p_dvd->i_title_start = vts.i_pos + DVD_LB_SIZE *
651                       (off_t)( vts.manager_inf.i_title_vob_start_sector );
652
653         /* last video cell */
654         p_dvd->i_cell = 0;
655         p_dvd->i_prg_cell = -1 +
656             vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
657
658         if( DVDFindCell( p_dvd ) < 0 )
659         {
660             intf_ErrMsg( "dvd error: can't find title end" );
661             p_input->b_error = 1;
662             return -1;
663         }
664
665         /* temporary hack to fix size in some dvds */
666         if( p_dvd->i_cell >= vts.cell_inf.i_cell_nb )
667         {
668             p_dvd->i_cell = vts.cell_inf.i_cell_nb - 1;
669         }
670
671         p_dvd->i_sector = 0;
672         p_dvd->i_size = DVD_LB_SIZE *
673           (off_t)( vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
674         intf_WarnMsg( 2, "dvd info: stream size 1: %lld @ %d", p_dvd->i_size,
675                       vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
676
677         if( DVDChapterSelect( p_dvd, 1 ) < 0 )
678         {
679             intf_ErrMsg( "dvd error: can't find first chapter" );
680             p_input->b_error = 1;
681             return -1;
682         }
683
684         p_dvd->i_size -= (off_t)( p_dvd->i_sector + 1 ) *DVD_LB_SIZE;
685
686         intf_WarnMsg( 2, "dvd info: title: %d", p_dvd->i_title );
687         intf_WarnMsg( 2, "dvd info: vobstart at: %lld", p_dvd->i_start );
688         intf_WarnMsg( 2, "dvd info: stream size: %lld", p_dvd->i_size );
689         intf_WarnMsg( 2, "dvd info: number of chapters: %d",
690                    vmg.title_inf.p_attr[p_dvd->i_title-1].i_chapter_nb );
691         intf_WarnMsg( 2, "dvd info: number of angles: %d", p_dvd->i_angle_nb );
692
693 //        vlc_mutex_lock( &p_input->stream.stream_lock );
694
695         /* Area definition */
696         p_input->stream.p_selected_area->i_start = p_dvd->i_start;
697         p_input->stream.p_selected_area->i_size = p_dvd->i_size;
698         p_input->stream.p_selected_area->i_angle_nb = p_dvd->i_angle_nb;
699         p_input->stream.p_selected_area->i_angle = p_dvd->i_angle;
700
701         /*
702          * Destroy obsolete ES by reinitializing program 0
703          * and find all ES in title with ifo data
704          */
705         if( p_input->stream.pp_programs != NULL )
706         {
707             /* We don't use input_EndStream here since
708              * we keep area structures */
709
710             for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
711             {
712                 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
713             }
714
715             input_DelProgram( p_input, p_input->stream.pp_programs[0] );
716
717             p_input->stream.pp_selected_es = NULL;
718             p_input->stream.i_selected_es_number = 0;
719         }
720
721         input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
722
723         /* No PSM to read in DVD mode, we already have all information */
724         p_input->stream.pp_programs[0]->b_is_ok = 1;
725         p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_START;
726
727         p_es = NULL;
728
729         /* ES 0 -> video MPEG2 */
730         p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
731         p_es->i_stream_id = 0xe0;
732         p_es->i_type = MPEG2_VIDEO_ES;
733         p_es->i_cat = VIDEO_ES;
734         intf_WarnMsg( 1, "dvd info: video mpeg2 stream" );
735         if( p_main->b_video )
736         {
737             input_SelectES( p_input, p_es );
738         }
739
740         /* Audio ES, in the order they appear in .ifo */
741             
742         i_ac3 = 0x7f;
743         i_mpeg = 0xc0;
744         i_lpcm = 0x9f;
745
746         for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
747         {
748
749         intf_WarnMsg( 5, "dvd info: audio %d: %x %x %x %x %x %x %x %x %x %x %x %x", i,
750             vts.manager_inf.p_audio_attr[i-1].i_num_channels,
751             vts.manager_inf.p_audio_attr[i-1].i_coding_mode,
752             vts.manager_inf.p_audio_attr[i-1].i_multichannel_extension,
753             vts.manager_inf.p_audio_attr[i-1].i_type,
754             vts.manager_inf.p_audio_attr[i-1].i_appl_mode,
755             vts.manager_inf.p_audio_attr[i-1].i_foo,
756             vts.manager_inf.p_audio_attr[i-1].i_test,
757             vts.manager_inf.p_audio_attr[i-1].i_bar,
758             vts.manager_inf.p_audio_attr[i-1].i_quantization,
759             vts.manager_inf.p_audio_attr[i-1].i_sample_freq,
760             vts.manager_inf.p_audio_attr[i-1].i_lang_code,
761             vts.manager_inf.p_audio_attr[i-1].i_caption );
762
763             switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
764             {
765             case 0x00:              /* AC3 */
766                 i_id = ( ( i_ac3 + i ) << 8 ) | 0xbd;
767                 p_es = input_AddES( p_input,
768                                     p_input->stream.pp_programs[0], i_id, 0 );
769                 p_es->i_stream_id = 0xbd;
770                 p_es->i_type = AC3_AUDIO_ES;
771                 p_es->b_audio = 1;
772                 p_es->i_cat = AUDIO_ES;
773                 strcpy( p_es->psz_desc, Language( hton16(
774                     vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
775                 strcat( p_es->psz_desc, " (ac3)" );
776
777                 intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
778                               i, p_es->psz_desc, i_id );
779
780                 break;
781             case 0x02:
782             case 0x03:              /* MPEG audio */
783                 i_id = 0xbf + i;
784                 p_es = input_AddES( p_input,
785                                     p_input->stream.pp_programs[0], i_id, 0 );
786                 p_es->i_stream_id = i_id;
787                 p_es->i_type = MPEG2_AUDIO_ES;
788                 p_es->b_audio = 1;
789                 p_es->i_cat = AUDIO_ES;
790                 strcpy( p_es->psz_desc, Language( hton16(
791                     vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
792                 strcat( p_es->psz_desc, " (mpeg)" );
793
794                 intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
795                               i, p_es->psz_desc, i_id );
796
797                 break;
798             case 0x04:              /* LPCM */
799 #if 0
800                 i_id = ( ( i_lpcm + i ) << 8 ) | 0xbd;
801                 p_es = input_AddES( p_input,
802                                     p_input->stream.pp_programs[0], i_id, 0 );
803                 p_es->i_stream_id = i_id;
804                 p_es->i_type = LPCM_AUDIO_ES;
805                 p_es->b_audio = 1;
806                 p_es->i_cat = AUDIO_ES;
807                 strcpy( p_es->psz_desc, Language( hton16(
808                     vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
809                 strcat( p_es->psz_desc, " (lpcm)" );
810
811                 intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
812                               i, p_es->psz_desc, i_id );
813 #else
814                 i_id = 0;
815                 intf_ErrMsg( "dvd warning: LPCM audio not handled yet" );
816 #endif
817                 break;
818             case 0x06:              /* DTS */
819                 i_id = 0;
820                 i_ac3--;
821                 intf_ErrMsg( "dvd warning: DTS audio not handled yet" );
822                 break;
823             default:
824                 i_id = 0;
825                 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
826                          vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
827             }
828         
829         }
830     
831         /* Sub Picture ES */
832            
833         b_last = 0;
834         i_sub_pic = 0x20;
835         for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
836         {
837             if( !b_last )
838             {
839                 i_id = ( i_sub_pic++ << 8 ) | 0xbd;
840                 p_es = input_AddES( p_input,
841                                     p_input->stream.pp_programs[0], i_id, 0 );
842                 p_es->i_stream_id = 0xbd;
843                 p_es->i_type = DVD_SPU_ES;
844                 p_es->i_cat = SPU_ES;
845                 strcpy( p_es->psz_desc, Language( hton16(
846                     vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) ); 
847                 intf_WarnMsg( 3, "dvd info: spu stream %d %s\t(0x%x)",
848                               i, p_es->psz_desc, i_id );
849     
850                 /* The before the last spu has a 0x0 prefix */
851                 b_last =
852                     ( vts.manager_inf.p_spu_attr[i].i_prefix == 0 ); 
853             }
854         }
855
856         if( p_main->b_audio )
857         {
858             /* For audio: first one if none or a not existing one specified */
859             i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
860             if( i_audio < 0 || i_audio > vts.manager_inf.i_audio_nb )
861             {
862                 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
863                 i_audio = 1;
864             }
865             if( i_audio > 0 && vts.manager_inf.i_audio_nb > 0 )
866             {
867                 input_SelectES( p_input, p_input->stream.pp_es[i_audio] );
868             }
869         }
870
871         if( p_main->b_video )
872         {
873             /* for spu, default is none */
874             i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
875             if( i_spu < 0 || i_spu > vts.manager_inf.i_spu_nb )
876             {
877                 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
878                 i_spu = 0;
879             }
880             if( i_spu > 0 && vts.manager_inf.i_spu_nb > 0 )
881             {
882                 i_spu += vts.manager_inf.i_audio_nb;
883                 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
884             }
885         }
886     } /* i_title >= 0 */
887     else
888     {
889         p_area = p_input->stream.p_selected_area;
890     }
891 #undef vts
892 #undef vmg
893
894     /*
895      * Chapter selection
896      */
897
898     
899     if( p_area->i_part != p_dvd->i_chapter )
900     {
901         if( ( p_area->i_part > 0 ) &&
902             ( p_area->i_part <= p_area->i_part_nb ))
903         {
904             if( DVDChapterSelect( p_dvd, p_area->i_part ) < 0 )
905             {
906                 intf_ErrMsg( "dvd error: can't set chapter in area" );
907                 p_input->b_error = 1;
908                 return -1;
909             }
910     
911             p_input->stream.p_selected_area->i_tell = p_dvd->i_start -
912                                                       p_area->i_start;
913             p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
914     
915             intf_WarnMsg( 2, "dvd info: chapter %d start at: %lld",
916                                         p_area->i_part, p_area->i_tell );
917         }
918         else
919         {
920             p_area->i_part = 1;
921             p_dvd->i_chapter = 1;
922         }
923     }
924 #define title \
925     p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
926
927     if( p_area->i_angle != p_dvd->i_angle )
928     {
929         if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
930         {
931             if( ( p_area->i_angle - p_dvd->i_angle ) < 0 )
932             {
933                 p_dvd->i_cell = 0;
934             }
935             p_dvd->i_prg_cell += ( p_area->i_angle - p_dvd->i_angle );
936             p_dvd->i_angle = p_area->i_angle;
937     
938             DVDFindSector( p_dvd );
939             p_dvd->i_cell += p_dvd->i_angle_cell;
940         }
941         else
942         {
943             p_dvd->i_angle = p_area->i_angle;
944         }
945
946         intf_WarnMsg( 2, "dvd info: angle %d selected", p_area->i_angle );
947     }
948
949     /* warn interface that something has changed */
950     p_input->stream.b_changed = 1;
951
952     vlc_mutex_unlock( &p_input->stream.stream_lock );
953
954     return 0;
955 }
956
957
958 /*****************************************************************************
959  * DVDRead: reads data packets into the netlist.
960  *****************************************************************************
961  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
962  * EOF.
963  *****************************************************************************/
964 static int DVDRead( input_thread_t * p_input,
965                     data_packet_t ** pp_packets )
966 {
967     thread_dvd_data_t *     p_dvd;
968     dvd_netlist_t *         p_netlist;
969     input_area_t *          p_area;
970     struct iovec *          p_vec;
971     struct data_packet_s *  pp_data[p_input->i_read_once];
972     u8 *                    pi_cur;
973     int                     i_block_once;
974     int                     i_packet_size;
975     int                     i_iovec;
976     int                     i_packet;
977     int                     i_pos;
978     int                     i_read_bytes;
979     int                     i_read_blocks;
980     off_t                   i_off;
981     boolean_t               b_eof;
982     boolean_t               b_eot;
983
984     p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
985     p_netlist = (dvd_netlist_t *)p_input->p_method_data;
986
987     /* Get an iovec pointer */
988     if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
989     {
990         intf_ErrMsg( "dvd error: can't get iovec" );
991         return -1;
992     }
993
994     i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
995
996     /* Get the position of the next cell if we're at cell end */
997     if( i_block_once <= 0 )
998     {
999         int     i_angle;
1000
1001         p_dvd->i_cell++;
1002         p_dvd->i_angle_cell++;
1003
1004         /* Find cell index in adress map */
1005         if( DVDFindSector( p_dvd ) < 0 )
1006         {
1007             pp_packets[0] = NULL;
1008             intf_ErrMsg( "dvd error: can't find next cell" );
1009             return 1;
1010         }
1011
1012         /* Position the fd pointer on the right address */
1013         i_off = lseek( p_dvd->i_fd,
1014                        p_dvd->i_title_start +
1015                        (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET );
1016
1017         /* update chapter : it will be easier when we have navigation
1018          * ES support */
1019         if( p_dvd->i_chapter < ( p_dvd->i_chapter_nb - 1 ) )
1020         {
1021             if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1022             {
1023                 i_angle = p_dvd->i_angle - 1;
1024             }
1025             else
1026             {
1027                 i_angle = 0;
1028             }
1029             if( title.chapter_map.pi_start_cell[p_dvd->i_chapter] <=
1030                 ( p_dvd->i_prg_cell - i_angle + 1 ) )
1031             {
1032                 p_dvd->i_chapter++;
1033             }
1034         }
1035
1036         vlc_mutex_lock( &p_input->stream.stream_lock );
1037
1038         p_input->stream.p_selected_area->i_tell = i_off -
1039                                     p_input->stream.p_selected_area->i_start;
1040         p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1041
1042         /* the synchro has to be reinitialized when we change cell */
1043         p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_REINIT;
1044
1045         vlc_mutex_unlock( &p_input->stream.stream_lock );
1046
1047         i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
1048     }
1049
1050     /* the number of blocks read is the maw between the requested
1051      * value and the leaving block in the cell */
1052     if( i_block_once > p_dvd->i_block_once )
1053     {
1054         i_block_once = p_dvd->i_block_once;
1055     }
1056 //intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
1057
1058     p_netlist->i_read_once = i_block_once;
1059
1060     /* Reads from DVD */
1061     i_read_bytes = readv( p_dvd->i_fd, p_vec, i_block_once );
1062     i_read_blocks = ( i_read_bytes + 0x7ff ) >> 11;
1063
1064     /* Update netlist indexes */
1065     DVDMviovec( p_netlist, i_read_blocks, pp_data );
1066
1067     /* Update global position */
1068     p_dvd->i_sector += i_read_blocks;
1069
1070     i_packet = 0;
1071
1072     /* Read headers to compute payload length */
1073     for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
1074     {
1075         if( p_dvd->b_encrypted )
1076         {
1077             CSSDescrambleSector( p_dvd->p_css->pi_title_key, 
1078                                  p_vec[i_iovec].iov_base );
1079             ((u8*)(p_vec[i_iovec].iov_base))[0x14] &= 0x8F;
1080         }
1081
1082         i_pos = 0;
1083
1084         while( i_pos < p_netlist->i_buffer_size )
1085         {
1086             pi_cur = (u8*)(p_vec[i_iovec].iov_base + i_pos);
1087
1088             /*default header */
1089             if( U32_AT( pi_cur ) != 0x1BA )
1090             {
1091                 /* That's the case for all packets, except pack header. */
1092                 i_packet_size = U16_AT( pi_cur + 4 );
1093                 pp_packets[i_packet] = DVDNewPtr( p_netlist );
1094             }
1095             else
1096             {
1097                 /* Pack header. */
1098                 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
1099                 {
1100                     /* MPEG-2 */
1101                     i_packet_size = 8;
1102                 }
1103                 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
1104                 {
1105                     /* MPEG-1 */
1106                     i_packet_size = 6;
1107                 }
1108                 else
1109                 {
1110                     intf_ErrMsg( "Unable to determine stream type" );
1111                     return( -1 );
1112                 }
1113
1114                 pp_packets[i_packet] = pp_data[i_iovec];
1115
1116             }
1117
1118             (*pp_data[i_iovec]->pi_refcount)++;
1119
1120             pp_packets[i_packet]->pi_refcount = pp_data[i_iovec]->pi_refcount;
1121
1122             pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
1123
1124             pp_packets[i_packet]->p_payload_start =
1125                     pp_packets[i_packet]->p_buffer + i_pos;
1126
1127             pp_packets[i_packet]->p_payload_end =
1128                     pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
1129
1130             pp_packets[i_packet]->p_next = NULL;
1131             pp_packets[i_packet]->b_discard_payload = 0;
1132
1133             i_packet++;
1134             i_pos += i_packet_size + 6;
1135         }
1136     }
1137
1138     pp_packets[i_packet] = NULL;
1139
1140     vlc_mutex_lock( &p_input->stream.stream_lock );
1141
1142     p_input->stream.p_selected_area->i_tell += i_read_bytes;
1143     b_eot = !( p_input->stream.p_selected_area->i_tell < p_dvd->i_size );
1144     b_eof = b_eot && ( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb );
1145     p_area = p_input->stream.pp_areas[p_dvd->i_title + 1];
1146
1147     vlc_mutex_unlock( &p_input->stream.stream_lock );
1148
1149     if( b_eof )
1150     {
1151         return 1;
1152     }
1153
1154     if( b_eot )
1155     {
1156         p_dvd->i_title++;
1157         DVDSetArea( p_input, p_area );
1158         return 0;
1159     }
1160
1161     if( i_read_blocks == i_block_once )
1162     {
1163         return 0;
1164     }
1165
1166     return -1;
1167 }
1168
1169 /*****************************************************************************
1170  * DVDRewind : reads a stream backward
1171  *****************************************************************************/
1172 static int DVDRewind( input_thread_t * p_input )
1173 {
1174     return( -1 );
1175 }
1176
1177 /*****************************************************************************
1178  * DVDSeek : Goes to a given position on the stream ; this one is used by the 
1179  * input and translate chronological position from input to logical postion
1180  * on the device
1181  * ---
1182  * The lock should be taken before calling this function.
1183  *****************************************************************************/
1184 static void DVDSeek( input_thread_t * p_input, off_t i_off )
1185 {
1186     thread_dvd_data_t *     p_dvd;
1187     off_t                   i_pos;
1188     int                     i_prg_cell;
1189     int                     i_cell;
1190     int                     i_chapter;
1191     int                     i_angle;
1192     
1193     p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1194
1195     /* we have to take care of offset of beginning of title */
1196     i_pos = i_off + p_input->stream.p_selected_area->i_start
1197                   - p_dvd->i_title_start;
1198
1199     /* update navigation data */
1200     p_dvd->i_sector = i_pos >> 11;
1201
1202     i_prg_cell = 0;
1203     i_chapter = 0;
1204
1205     /* parse vobu address map to find program cell */
1206     while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector  )
1207     {
1208         i_prg_cell++;
1209     }
1210
1211     p_dvd->i_prg_cell = i_prg_cell;
1212
1213     if( DVDChooseAngle( p_dvd ) < 0 )
1214     {
1215         p_input->b_error = 1;
1216         return;        
1217     }
1218
1219     p_dvd->i_cell = 0;
1220
1221     /* Find first title cell which is inside program cell */
1222     if( DVDFindCell( p_dvd ) < 0 )
1223     {
1224         /* no following cell : we're at eof */
1225         intf_ErrMsg( "dvd error: cell seeking failed" );
1226         p_input->b_error = 1;
1227         return;
1228     }
1229
1230     i_cell = p_dvd->i_cell;
1231
1232 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1233     /* parse cell address map to find title cell containing sector */
1234     while( cell.i_end_sector < p_dvd->i_sector )
1235     {
1236         i_cell++;
1237     }
1238
1239     p_dvd->i_cell = i_cell;
1240
1241     /* if we're inside a multi-angle zone, we have to choose i_sector
1242      * in the current angle ; we can't do it all the time since cells
1243      * can be very wide out of such zones */
1244     if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1245     {
1246         p_dvd->i_sector = MAX(
1247                 cell.i_start_sector,
1248                 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1249     }
1250
1251     p_dvd->i_end_sector = MIN(
1252             cell.i_end_sector,
1253             title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1254 #undef cell
1255     /* update chapter */
1256     if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1257     {
1258         i_angle = p_dvd->i_angle - 1;
1259     }
1260     else
1261     {
1262         i_angle = 0;
1263     }
1264     while( ( title.chapter_map.pi_start_cell[i_chapter] <=
1265                 ( p_dvd->i_prg_cell - i_angle + 1 ) ) &&
1266            ( i_chapter < ( p_dvd->i_chapter_nb - 1 ) ) )
1267     {
1268         i_chapter++;
1269     }
1270
1271     p_dvd->i_chapter = i_chapter;
1272     p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1273
1274     p_input->stream.p_selected_area->i_tell =
1275                 lseek( p_dvd->i_fd, p_dvd->i_title_start +
1276                        (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET ) -
1277                 p_input->stream.p_selected_area->i_start;
1278 /*
1279     intf_WarnMsg( 3, "Program Cell: %d Cell: %d Chapter: %d",
1280                      p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
1281 */
1282
1283     return;
1284 }
1285
1286 #define cell  p_dvd->p_ifo->vts.cell_inf
1287
1288 /*****************************************************************************
1289  * DVDFindCell: adjust the title cell index with the program cell
1290  *****************************************************************************/
1291 static int DVDFindCell( thread_dvd_data_t * p_dvd )
1292 {
1293     int                 i_cell;
1294     int                 i_index;
1295
1296     i_cell = p_dvd->i_cell;
1297     i_index = p_dvd->i_prg_cell;
1298
1299     if( i_cell >= cell.i_cell_nb )
1300     {
1301         return -1;
1302     }
1303
1304     while( ( ( title.p_cell_pos[i_index].i_vob_id !=
1305                    cell.p_cell_map[i_cell].i_vob_id ) ||
1306       ( title.p_cell_pos[i_index].i_cell_id !=
1307                    cell.p_cell_map[i_cell].i_cell_id ) ) &&
1308            ( i_cell < cell.i_cell_nb - 1 ) )
1309     {
1310         i_cell++;
1311     }
1312
1313 /*
1314 intf_WarnMsg( 3, "FindCell: i_cell %d i_index %d found %d nb %d",
1315                     p_dvd->i_cell,
1316                     p_dvd->i_prg_cell,
1317                     i_cell,
1318                     cell.i_cell_nb );
1319 */
1320
1321     p_dvd->i_cell = i_cell;
1322
1323     return 0;    
1324 }
1325
1326 #undef cell
1327
1328 /*****************************************************************************
1329  * DVDFindSector: find cell index in adress map from index in
1330  * information table program map and give corresponding sectors.
1331  *****************************************************************************/
1332 static int DVDFindSector( thread_dvd_data_t * p_dvd )
1333 {
1334
1335     if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
1336     {
1337         p_dvd->i_prg_cell++;
1338
1339         if( DVDChooseAngle( p_dvd ) < 0 )
1340         {
1341             return -1;
1342         }
1343     }
1344
1345     if( DVDFindCell( p_dvd ) < 0 )
1346     {
1347         intf_ErrMsg( "dvd error: can't find sector" );
1348         return -1;
1349     }
1350
1351     /* Find start and end sectors of new cell */
1352 #if 1
1353     p_dvd->i_sector = MAX(
1354          p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1355          title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1356     p_dvd->i_end_sector = MIN(
1357          p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1358          title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1359 #else
1360     p_dvd->i_sector = title.p_cell_play[p_dvd->i_prg_cell].i_start_sector;
1361     p_dvd->i_end_sector = title.p_cell_play[p_dvd->i_prg_cell].i_end_sector;
1362 #endif
1363
1364 /*
1365     intf_WarnMsg( 3, "cell: %d sector1: 0x%x end1: 0x%x\n"
1366                    "index: %d sector2: 0x%x end2: 0x%x\n"
1367                    "category: 0x%x ilvu end: 0x%x vobu start 0x%x", 
1368         p_dvd->i_cell,
1369         p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1370         p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1371         p_dvd->i_prg_cell,
1372         title.p_cell_play[p_dvd->i_prg_cell].i_start_sector,
1373         title.p_cell_play[p_dvd->i_prg_cell].i_end_sector,
1374         title.p_cell_play[p_dvd->i_prg_cell].i_category, 
1375         title.p_cell_play[p_dvd->i_prg_cell].i_first_ilvu_vobu_esector,
1376         title.p_cell_play[p_dvd->i_prg_cell].i_last_vobu_start_sector );
1377 */
1378
1379     return 0;
1380 }
1381
1382 /*****************************************************************************
1383  * DVDChapterSelect: find the cell corresponding to requested chapter
1384  *****************************************************************************/
1385 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
1386 {
1387
1388     /* Find cell index in Program chain for current chapter */
1389     p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
1390     p_dvd->i_cell = 0;
1391     p_dvd->i_sector = 0;
1392
1393     DVDChooseAngle( p_dvd );
1394
1395     /* Search for cell_index in cell adress_table and initialize
1396      * start sector */
1397     if( DVDFindSector( p_dvd ) < 0 )
1398     {
1399         intf_ErrMsg( "dvd error: can't select chapter" );
1400         return -1;
1401     }
1402
1403     /* start is : beginning of vts vobs + offset to vob x */
1404     p_dvd->i_start = p_dvd->i_title_start +
1405                          DVD_LB_SIZE * (off_t)( p_dvd->i_sector );
1406
1407     /* Position the fd pointer on the right address */
1408     p_dvd->i_start = lseek( p_dvd->i_fd, p_dvd->i_start, SEEK_SET );
1409
1410     p_dvd->i_chapter = i_chapter;
1411     return 0;
1412 }
1413
1414 /*****************************************************************************
1415  * DVDChooseAngle: select the cell corresponding to the selected angle
1416  *****************************************************************************/
1417 static int DVDChooseAngle( thread_dvd_data_t * p_dvd )
1418 {
1419     /* basic handling of angles */
1420     switch( ( ( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1421                     >> 12 ) )
1422     {
1423         /* we enter a muli-angle section */
1424         case 0x5:
1425             p_dvd->i_prg_cell += p_dvd->i_angle - 1;
1426             p_dvd->i_angle_cell = 0;
1427 //            intf_WarnMsg( 3, "dvd info: choosing angle %d", p_dvd->i_angle );
1428             break;
1429         /* we exit a multi-angle section */
1430         case 0x9:
1431         case 0xd:
1432             p_dvd->i_prg_cell += p_dvd->i_angle_nb - p_dvd->i_angle;
1433             break;
1434     }
1435
1436     return 0;
1437 }
1438
1439 #undef title