]> git.sesse.net Git - vlc/blob - plugins/dvd/input_dvd.c
*Much work on the gnome interface: added oct's playlist from gtk plugin,
[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.55 2001/05/06 18:32:30 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 #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( 1, "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                         
672         p_dvd->i_sector = 0;
673         p_dvd->i_size = DVD_LB_SIZE *
674           (off_t)( vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
675         intf_WarnMsg( 2, "dvd info: stream size 1: %lld @ %d", p_dvd->i_size,
676                       vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
677         
678         if( DVDChapterSelect( p_dvd, 1 ) < 0 )
679         {
680             intf_ErrMsg( "dvd error: can't find first chapter" );
681             p_input->b_error = 1;
682             return -1;
683         }
684
685         p_dvd->i_size -= (off_t)( p_dvd->i_sector + 1 ) *DVD_LB_SIZE;
686
687         intf_WarnMsg( 2, "dvd info: title: %d", p_dvd->i_title );
688         intf_WarnMsg( 2, "dvd info: vobstart at: %lld", p_dvd->i_start );
689         intf_WarnMsg( 2, "dvd info: stream size: %lld", p_dvd->i_size );
690         intf_WarnMsg( 2, "dvd info: number of chapters: %d",
691                    vmg.title_inf.p_attr[p_dvd->i_title-1].i_chapter_nb );
692         intf_WarnMsg( 2, "dvd info: number of angles: %d", p_dvd->i_angle_nb );
693
694 //        vlc_mutex_lock( &p_input->stream.stream_lock );
695
696         /* Area definition */
697         p_input->stream.p_selected_area->i_start = p_dvd->i_start;
698         p_input->stream.p_selected_area->i_size = p_dvd->i_size;
699         p_input->stream.p_selected_area->i_angle_nb = p_dvd->i_angle_nb;
700         p_input->stream.p_selected_area->i_angle = p_dvd->i_angle;
701
702         /*
703          * Destroy obsolete ES by reinitializing program 0
704          * and find all ES in title with ifo data
705          */
706         if( p_input->stream.pp_programs != NULL )
707         {
708             /* We don't use input_EndStream here since
709              * we keep area structures */
710
711             for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
712             {
713                 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
714             }
715
716             input_DelProgram( p_input, p_input->stream.pp_programs[0] );
717
718             p_input->stream.pp_selected_es = NULL;
719             p_input->stream.i_selected_es_number = 0;
720         }
721
722         input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
723
724         /* No PSM to read in DVD mode, we already have all information */
725         p_input->stream.pp_programs[0]->b_is_ok = 1;
726         p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_START;
727
728         p_es = NULL;
729
730         /* ES 0 -> video MPEG2 */
731         p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
732         p_es->i_stream_id = 0xe0;
733         p_es->i_type = MPEG2_VIDEO_ES;
734         p_es->i_cat = VIDEO_ES;
735         intf_WarnMsg( 1, "dvd info: video mpeg2 stream" );
736         if( p_main->b_video )
737         {
738             input_SelectES( p_input, p_es );
739         }
740
741         /* Audio ES, in the order they appear in .ifo */
742             
743         i_ac3 = 0x7f;
744         i_mpeg = 0xc0;
745         i_lpcm = 0x9f;
746
747         for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
748         {
749
750         intf_WarnMsg( 1, "Audio %d: %x %x %x %x %x %x %x %x %x %x %x %x", i,
751             vts.manager_inf.p_audio_attr[i-1].i_num_channels,
752             vts.manager_inf.p_audio_attr[i-1].i_coding_mode,
753             vts.manager_inf.p_audio_attr[i-1].i_multichannel_extension,
754             vts.manager_inf.p_audio_attr[i-1].i_type,
755             vts.manager_inf.p_audio_attr[i-1].i_appl_mode,
756             vts.manager_inf.p_audio_attr[i-1].i_foo,
757             vts.manager_inf.p_audio_attr[i-1].i_test,
758             vts.manager_inf.p_audio_attr[i-1].i_bar,
759             vts.manager_inf.p_audio_attr[i-1].i_quantization,
760             vts.manager_inf.p_audio_attr[i-1].i_sample_freq,
761             vts.manager_inf.p_audio_attr[i-1].i_lang_code,
762             vts.manager_inf.p_audio_attr[i-1].i_caption );
763
764             switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
765             {
766             case 0x00:              /* AC3 */
767                 i_id = ( ( i_ac3 + i ) << 8 ) | 0xbd;
768                 p_es = input_AddES( p_input,
769                                     p_input->stream.pp_programs[0], i_id, 0 );
770                 p_es->i_stream_id = 0xbd;
771                 p_es->i_type = AC3_AUDIO_ES;
772                 p_es->b_audio = 1;
773                 p_es->i_cat = AUDIO_ES;
774                 strcpy( p_es->psz_desc, Language( hton16(
775                     vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
776                 strcat( p_es->psz_desc, " (ac3)" );
777
778                 intf_WarnMsg( 1, "dvd info: audio stream %d %s\t(0x%x)",
779                               i, p_es->psz_desc, i_id );
780
781                 break;
782             case 0x02:
783             case 0x03:              /* MPEG audio */
784                 i_id = 0xbf + i;
785                 p_es = input_AddES( p_input,
786                                     p_input->stream.pp_programs[0], i_id, 0 );
787                 p_es->i_stream_id = i_id;
788                 p_es->i_type = MPEG2_AUDIO_ES;
789                 p_es->b_audio = 1;
790                 p_es->i_cat = AUDIO_ES;
791                 strcpy( p_es->psz_desc, Language( hton16(
792                     vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
793                 strcat( p_es->psz_desc, " (mpeg)" );
794
795                 intf_WarnMsg( 1, "dvd info: audio stream %d %s\t(0x%x)",
796                               i, p_es->psz_desc, i_id );
797
798                 break;
799             case 0x04:              /* LPCM */
800 #if 0
801                 i_id = ( ( i_lpcm + i ) << 8 ) | 0xbd;
802                 p_es = input_AddES( p_input,
803                                     p_input->stream.pp_programs[0], i_id, 0 );
804                 p_es->i_stream_id = i_id;
805                 p_es->i_type = LPCM_AUDIO_ES;
806                 p_es->b_audio = 1;
807                 p_es->i_cat = AUDIO_ES;
808                 strcpy( p_es->psz_desc, Language( hton16(
809                     vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
810                 strcat( p_es->psz_desc, " (lpcm)" );
811
812                 intf_WarnMsg( 1, "dvd info: audio stream %d %s\t(0x%x)",
813                               i, p_es->psz_desc, i_id );
814 #else
815                 i_id = 0;
816                 intf_ErrMsg( "dvd warning: LPCM audio not handled yet" );
817 #endif
818                 break;
819             case 0x06:              /* DTS */
820                 i_id = 0;
821                 i_ac3--;
822                 intf_ErrMsg( "dvd warning: DTS audio not handled yet" );
823                 break;
824             default:
825                 i_id = 0;
826                 intf_ErrMsg( "dvd warning: unknown audio type %.2x",
827                          vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
828             }
829         
830         }
831     
832         /* Sub Picture ES */
833            
834         b_last = 0;
835         i_sub_pic = 0x20;
836         for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
837         {
838             if( !b_last )
839             {
840                 i_id = ( i_sub_pic++ << 8 ) | 0xbd;
841                 p_es = input_AddES( p_input,
842                                     p_input->stream.pp_programs[0], i_id, 0 );
843                 p_es->i_stream_id = 0xbd;
844                 p_es->i_type = DVD_SPU_ES;
845                 p_es->i_cat = SPU_ES;
846                 strcpy( p_es->psz_desc, Language( hton16(
847                     vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) ); 
848                 intf_WarnMsg( 1, "dvd info: spu stream %d %s\t(0x%x)",
849                               i, p_es->psz_desc, i_id );
850     
851                 /* The before the last spu has a 0x0 prefix */
852                 b_last =
853                     ( vts.manager_inf.p_spu_attr[i].i_prefix == 0 ); 
854             }
855         }
856
857         if( p_main->b_audio )
858         {
859             /* For audio: first one if none or a not existing one specified */
860             i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
861             if( i_audio < 0 || i_audio > vts.manager_inf.i_audio_nb )
862             {
863                 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
864                 i_audio = 1;
865             }
866             if( i_audio > 0 && vts.manager_inf.i_audio_nb > 0 )
867             {
868                 input_SelectES( p_input, p_input->stream.pp_es[i_audio] );
869             }
870         }
871
872         if( p_main->b_video )
873         {
874             /* for spu, default is none */
875             i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
876             if( i_spu < 0 || i_spu > vts.manager_inf.i_spu_nb )
877             {
878                 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
879                 i_spu = 0;
880             }
881             if( i_spu > 0 && vts.manager_inf.i_spu_nb > 0 )
882             {
883                 i_spu += vts.manager_inf.i_audio_nb;
884                 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
885             }
886         }
887     } /* i_title >= 0 */
888     else
889     {
890         p_area = p_input->stream.p_selected_area;
891     }
892 #undef vts
893 #undef vmg
894
895     /*
896      * Chapter selection
897      */
898
899     
900     if( p_area->i_part != p_dvd->i_chapter )
901     {
902         if( ( p_area->i_part > 0 ) &&
903             ( p_area->i_part <= p_area->i_part_nb ))
904         {
905             if( DVDChapterSelect( p_dvd, p_area->i_part ) < 0 )
906             {
907                 intf_ErrMsg( "dvd error: can't set chapter in area" );
908                 p_input->b_error = 1;
909                 return -1;
910             }
911     
912             p_input->stream.p_selected_area->i_tell = p_dvd->i_start -
913                                                       p_area->i_start;
914             p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
915     
916             intf_WarnMsg( 2, "dvd info: chapter %d start at: %lld",
917                                         p_area->i_part, p_area->i_tell );
918         }
919         else
920         {
921             p_area->i_part = 1;
922             p_dvd->i_chapter = 1;
923         }
924     }
925 #define title \
926     p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
927
928     if( p_area->i_angle != p_dvd->i_angle )
929     {
930         if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
931         {
932             if( ( p_area->i_angle - p_dvd->i_angle ) < 0 )
933             {
934                 p_dvd->i_cell = 0;
935             }
936             p_dvd->i_prg_cell += ( p_area->i_angle - p_dvd->i_angle );
937             p_dvd->i_angle = p_area->i_angle;
938     
939             DVDFindSector( p_dvd );
940             p_dvd->i_cell += p_dvd->i_angle_cell;
941         }
942         else
943         {
944             p_dvd->i_angle = p_area->i_angle;
945         }
946
947         intf_WarnMsg( 2, "dvd info: angle %d selected", p_area->i_angle );
948     }
949
950     /* warn interface that something has changed */
951     p_input->stream.b_changed = 1;
952
953     vlc_mutex_unlock( &p_input->stream.stream_lock );
954
955     return 0;
956 }
957
958
959 /*****************************************************************************
960  * DVDRead: reads data packets into the netlist.
961  *****************************************************************************
962  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
963  * EOF.
964  *****************************************************************************/
965 static int DVDRead( input_thread_t * p_input,
966                     data_packet_t ** pp_packets )
967 {
968     thread_dvd_data_t *     p_dvd;
969     dvd_netlist_t *         p_netlist;
970     input_area_t *          p_area;
971     struct iovec *          p_vec;
972     struct data_packet_s *  pp_data[p_input->i_read_once];
973     u8 *                    pi_cur;
974     int                     i_block_once;
975     int                     i_packet_size;
976     int                     i_iovec;
977     int                     i_packet;
978     int                     i_pos;
979     int                     i_read_bytes;
980     int                     i_read_blocks;
981     off_t                   i_off;
982     boolean_t               b_eof;
983     boolean_t               b_eot;
984
985     p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
986     p_netlist = (dvd_netlist_t *)p_input->p_method_data;
987
988     /* Get an iovec pointer */
989     if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
990     {
991         intf_ErrMsg( "dvd error: can't get iovec" );
992         return -1;
993     }
994
995     i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
996
997     /* Get the position of the next cell if we're at cell end */
998     if( i_block_once <= 0 )
999     {
1000         int     i_angle;
1001
1002         p_dvd->i_cell++;
1003         p_dvd->i_angle_cell++;
1004
1005         /* Find cell index in adress map */
1006         if( DVDFindSector( p_dvd ) < 0 )
1007         {
1008             pp_packets[0] = NULL;
1009             intf_ErrMsg( "dvd error: can't find next cell" );
1010             return 1;
1011         }
1012
1013         /* Position the fd pointer on the right address */
1014         i_off = lseek( p_dvd->i_fd,
1015                        p_dvd->i_title_start +
1016                        (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET );
1017
1018         /* update chapter : it will be easier when we have navigation
1019          * ES support */
1020         if( p_dvd->i_chapter < ( p_dvd->i_chapter_nb - 1 ) )
1021         {
1022             if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1023             {
1024                 i_angle = p_dvd->i_angle - 1;
1025             }
1026             else
1027             {
1028                 i_angle = 0;
1029             }
1030             if( title.chapter_map.pi_start_cell[p_dvd->i_chapter] <=
1031                 ( p_dvd->i_prg_cell - i_angle + 1 ) )
1032             {
1033                 p_dvd->i_chapter++;
1034             }
1035         }
1036
1037         vlc_mutex_lock( &p_input->stream.stream_lock );
1038
1039         p_input->stream.p_selected_area->i_tell = i_off -
1040                                     p_input->stream.p_selected_area->i_start;
1041         p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1042
1043         /* the synchro has to be reinitialized when we change cell */
1044         p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_REINIT;
1045
1046         vlc_mutex_unlock( &p_input->stream.stream_lock );
1047
1048         i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
1049     }
1050
1051     /* the number of blocks read is the maw between the requested
1052      * value and the leaving block in the cell */
1053     if( i_block_once > p_dvd->i_block_once )
1054     {
1055         i_block_once = p_dvd->i_block_once;
1056     }
1057 //intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
1058
1059     p_netlist->i_read_once = i_block_once;
1060
1061     /* Reads from DVD */
1062     i_read_bytes = readv( p_dvd->i_fd, p_vec, i_block_once );
1063     i_read_blocks = ( i_read_bytes + 0x7ff ) >> 11;
1064
1065     /* Update netlist indexes */
1066     DVDMviovec( p_netlist, i_read_blocks, pp_data );
1067
1068     /* Update global position */
1069     p_dvd->i_sector += i_read_blocks;
1070
1071     i_packet = 0;
1072
1073     /* Read headers to compute payload length */
1074     for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
1075     {
1076         if( p_dvd->b_encrypted )
1077         {
1078             CSSDescrambleSector( p_dvd->p_css->pi_title_key, 
1079                                  p_vec[i_iovec].iov_base );
1080             ((u8*)(p_vec[i_iovec].iov_base))[0x14] &= 0x8F;
1081         }
1082
1083         i_pos = 0;
1084
1085         while( i_pos < p_netlist->i_buffer_size )
1086         {
1087             pi_cur = (u8*)(p_vec[i_iovec].iov_base + i_pos);
1088
1089             /*default header */
1090             if( U32_AT( pi_cur ) != 0x1BA )
1091             {
1092                 /* That's the case for all packets, except pack header. */
1093                 i_packet_size = U16_AT( pi_cur + 4 );
1094                 pp_packets[i_packet] = DVDNewPtr( p_netlist );
1095             }
1096             else
1097             {
1098                 /* Pack header. */
1099                 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
1100                 {
1101                     /* MPEG-2 */
1102                     i_packet_size = 8;
1103                 }
1104                 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
1105                 {
1106                     /* MPEG-1 */
1107                     i_packet_size = 6;
1108                 }
1109                 else
1110                 {
1111                     intf_ErrMsg( "Unable to determine stream type" );
1112                     return( -1 );
1113                 }
1114
1115                 pp_packets[i_packet] = pp_data[i_iovec];
1116
1117             }
1118
1119             (*pp_data[i_iovec]->pi_refcount)++;
1120
1121             pp_packets[i_packet]->pi_refcount = pp_data[i_iovec]->pi_refcount;
1122
1123             pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
1124
1125             pp_packets[i_packet]->p_payload_start =
1126                     pp_packets[i_packet]->p_buffer + i_pos;
1127
1128             pp_packets[i_packet]->p_payload_end =
1129                     pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
1130
1131             pp_packets[i_packet]->p_next = NULL;
1132             pp_packets[i_packet]->b_discard_payload = 0;
1133
1134             i_packet++;
1135             i_pos += i_packet_size + 6;
1136         }
1137     }
1138
1139     pp_packets[i_packet] = NULL;
1140
1141     vlc_mutex_lock( &p_input->stream.stream_lock );
1142
1143     p_input->stream.p_selected_area->i_tell += i_read_bytes;
1144     b_eot = !( p_input->stream.p_selected_area->i_tell < p_dvd->i_size );
1145     b_eof = b_eot && ( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb );
1146     p_area = p_input->stream.pp_areas[p_dvd->i_title + 1];
1147
1148     vlc_mutex_unlock( &p_input->stream.stream_lock );
1149
1150     if( b_eof )
1151     {
1152         return 1;
1153     }
1154
1155     if( b_eot )
1156     {
1157         p_dvd->i_title++;
1158         DVDSetArea( p_input, p_area );
1159         return 0;
1160     }
1161
1162     if( i_read_blocks == i_block_once )
1163     {
1164         return 0;
1165     }
1166
1167     return -1;
1168 }
1169
1170 /*****************************************************************************
1171  * DVDRewind : reads a stream backward
1172  *****************************************************************************/
1173 static int DVDRewind( input_thread_t * p_input )
1174 {
1175     return( -1 );
1176 }
1177
1178 /*****************************************************************************
1179  * DVDSeek : Goes to a given position on the stream ; this one is used by the 
1180  * input and translate chronological position from input to logical postion
1181  * on the device
1182  * ---
1183  * The lock should be taken before calling this function.
1184  *****************************************************************************/
1185 static void DVDSeek( input_thread_t * p_input, off_t i_off )
1186 {
1187     thread_dvd_data_t *     p_dvd;
1188     off_t                   i_pos;
1189     int                     i_prg_cell;
1190     int                     i_cell;
1191     int                     i_chapter;
1192     int                     i_angle;
1193     
1194     p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1195
1196     /* we have to take care of offset of beginning of title */
1197     i_pos = i_off + p_input->stream.p_selected_area->i_start
1198                   - p_dvd->i_title_start;
1199
1200     /* update navigation data */
1201     p_dvd->i_sector = i_pos >> 11;
1202
1203     i_prg_cell = 0;
1204     i_chapter = 0;
1205
1206     /* parse vobu address map to find program cell */
1207     while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector  )
1208     {
1209         i_prg_cell++;
1210     }
1211
1212     p_dvd->i_prg_cell = i_prg_cell;
1213
1214     if( DVDChooseAngle( p_dvd ) < 0 )
1215     {
1216         p_input->b_error = 1;
1217         return;        
1218     }
1219
1220     p_dvd->i_cell = 0;
1221
1222     /* Find first title cell which is inside program cell */
1223     if( DVDFindCell( p_dvd ) < 0 )
1224     {
1225         /* no following cell : we're at eof */
1226         intf_ErrMsg( "dvd error: cell seeking failed" );
1227         p_input->b_error = 1;
1228         return;
1229     }
1230
1231     i_cell = p_dvd->i_cell;
1232
1233 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1234     /* parse cell address map to find title cell containing sector */
1235     while( cell.i_end_sector < p_dvd->i_sector )
1236     {
1237         i_cell++;
1238     }
1239
1240     p_dvd->i_cell = i_cell;
1241
1242     /* if we're inside a multi-angle zone, we have to choose i_sector
1243      * in the current angle ; we can't do it all the time since cells
1244      * can be very wide out of such zones */
1245     if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1246     {
1247         p_dvd->i_sector = MAX(
1248                 cell.i_start_sector,
1249                 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1250     }
1251
1252     p_dvd->i_end_sector = MIN(
1253             cell.i_end_sector,
1254             title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1255 #undef cell
1256     /* update chapter */
1257     if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1258     {
1259         i_angle = p_dvd->i_angle - 1;
1260     }
1261     else
1262     {
1263         i_angle = 0;
1264     }
1265     while( ( title.chapter_map.pi_start_cell[i_chapter] <=
1266                 ( p_dvd->i_prg_cell - i_angle + 1 ) ) &&
1267            ( i_chapter < ( p_dvd->i_chapter_nb - 1 ) ) )
1268     {
1269         i_chapter++;
1270     }
1271
1272     p_dvd->i_chapter = i_chapter;
1273     p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1274
1275     p_input->stream.p_selected_area->i_tell =
1276                 lseek( p_dvd->i_fd, p_dvd->i_title_start +
1277                        (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET ) -
1278                 p_input->stream.p_selected_area->i_start;
1279 /*
1280     intf_WarnMsg( 1, "Program Cell: %d Cell: %d Chapter: %d",
1281                      p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
1282 */
1283
1284     return;
1285 }
1286
1287 #define cell  p_dvd->p_ifo->vts.cell_inf
1288                 
1289 /*****************************************************************************
1290  * DVDFindCell: adjust the title cell index with the program cell
1291  *****************************************************************************/
1292 static int DVDFindCell( thread_dvd_data_t * p_dvd )
1293 {
1294     int                 i_cell;
1295     int                 i_index;
1296
1297     i_cell = p_dvd->i_cell;
1298     i_index = p_dvd->i_prg_cell;
1299
1300     if( i_cell >= cell.i_cell_nb )
1301     {
1302         return -1;
1303     }
1304
1305     while( ( ( title.p_cell_pos[i_index].i_vob_id !=
1306                    cell.p_cell_map[i_cell].i_vob_id ) ||
1307       ( title.p_cell_pos[i_index].i_cell_id !=
1308                    cell.p_cell_map[i_cell].i_cell_id ) ) &&
1309            ( i_cell < cell.i_cell_nb - 1 ) )
1310     {
1311         i_cell++;
1312     }
1313
1314 /*
1315 intf_WarnMsg( 1, "FindCell: i_cell %d i_index %d found %d nb %d",
1316                     p_dvd->i_cell,
1317                     p_dvd->i_prg_cell,
1318                     i_cell,
1319                     cell.i_cell_nb );
1320 */
1321
1322     p_dvd->i_cell = i_cell;
1323
1324     return 0;    
1325 }
1326
1327 #undef cell
1328
1329 /*****************************************************************************
1330  * DVDFindSector: find cell index in adress map from index in
1331  * information table program map and give corresponding sectors.
1332  *****************************************************************************/
1333 static int DVDFindSector( thread_dvd_data_t * p_dvd )
1334 {
1335
1336     if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
1337     {
1338         p_dvd->i_prg_cell++;
1339
1340         if( DVDChooseAngle( p_dvd ) < 0 )
1341         {
1342             return -1;
1343         }
1344     }
1345
1346     if( DVDFindCell( p_dvd ) < 0 )
1347     {
1348         intf_ErrMsg( "dvd error: can't find sector" );
1349         return -1;
1350     }
1351
1352     /* Find start and end sectors of new cell */
1353 #if 1
1354     p_dvd->i_sector = MAX(
1355          p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1356          title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1357     p_dvd->i_end_sector = MIN(
1358          p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1359          title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1360 #else
1361     p_dvd->i_sector = title.p_cell_play[p_dvd->i_prg_cell].i_start_sector;
1362     p_dvd->i_end_sector = title.p_cell_play[p_dvd->i_prg_cell].i_end_sector;
1363 #endif
1364
1365 /*
1366     intf_WarnMsg( 1, "cell: %d sector1: 0x%x end1: 0x%x\n"
1367                    "index: %d sector2: 0x%x end2: 0x%x\n"
1368                    "category: 0x%x ilvu end: 0x%x vobu start 0x%x", 
1369         p_dvd->i_cell,
1370         p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1371         p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1372         p_dvd->i_prg_cell,
1373         title.p_cell_play[p_dvd->i_prg_cell].i_start_sector,
1374         title.p_cell_play[p_dvd->i_prg_cell].i_end_sector,
1375         title.p_cell_play[p_dvd->i_prg_cell].i_category, 
1376         title.p_cell_play[p_dvd->i_prg_cell].i_first_ilvu_vobu_esector,
1377         title.p_cell_play[p_dvd->i_prg_cell].i_last_vobu_start_sector );
1378 */
1379
1380     return 0;
1381 }
1382
1383 /*****************************************************************************
1384  * DVDChapterSelect: find the cell corresponding to requested chapter
1385  *****************************************************************************/
1386 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
1387 {
1388
1389     /* Find cell index in Program chain for current chapter */
1390     p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
1391     p_dvd->i_cell = 0;
1392     p_dvd->i_sector = 0;
1393
1394     DVDChooseAngle( p_dvd );
1395
1396     /* Search for cell_index in cell adress_table and initialize
1397      * start sector */
1398     if( DVDFindSector( p_dvd ) < 0 )
1399     {
1400         intf_ErrMsg( "dvd error: can't select chapter" );
1401         return -1;
1402     }
1403
1404     /* start is : beginning of vts vobs + offset to vob x */
1405     p_dvd->i_start = p_dvd->i_title_start +
1406                      DVD_LB_SIZE * (off_t)( p_dvd->i_sector );
1407
1408     /* Position the fd pointer on the right address */
1409     p_dvd->i_start = lseek( p_dvd->i_fd, p_dvd->i_start, SEEK_SET );
1410
1411     p_dvd->i_chapter = i_chapter;
1412     return 0;
1413 }
1414
1415 /*****************************************************************************
1416  * DVDChooseAngle: select the cell corresponding to the selected angle
1417  *****************************************************************************/
1418 static int DVDChooseAngle( thread_dvd_data_t * p_dvd )
1419 {
1420     /* basic handling of angles */
1421     switch( ( ( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1422                     >> 12 ) )
1423     {
1424         /* we enter a muli-angle section */
1425         case 0x5:
1426             p_dvd->i_prg_cell += p_dvd->i_angle - 1;
1427             p_dvd->i_angle_cell = 0;
1428 //            intf_WarnMsg( 1, "dvd info: choosing angle %d", p_dvd->i_angle );
1429             break;
1430         /* we exit a multi-angle section */
1431         case 0x9:
1432         case 0xd:
1433             p_dvd->i_prg_cell += p_dvd->i_angle_nb - p_dvd->i_angle;
1434             break;
1435     }
1436
1437     return 0;
1438 }
1439
1440 #undef title