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