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