]> git.sesse.net Git - vlc/blob - plugins/dvd/input_dvd.c
* Fixed the BeOS compile typo.
[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.61 2001/05/30 17:03:12 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 "dvd_summary.h"
79 #include "input_dvd.h"
80 #include "mpeg_system.h"
81
82 #include "debug.h"
83
84 #include "modules.h"
85
86 /*****************************************************************************
87  * Local prototypes
88  *****************************************************************************/
89 /* called from outside */
90 static int  DVDProbe    ( probedata_t *p_data );
91 static void DVDInit     ( struct input_thread_s * );
92 static void DVDEnd      ( struct input_thread_s * );
93 static int  DVDSetArea  ( struct input_thread_s *, struct input_area_s * );
94 static int  DVDRead     ( struct input_thread_s *, data_packet_t ** );
95 static void DVDSeek     ( struct input_thread_s *, off_t );
96 static int  DVDRewind   ( struct input_thread_s * );
97
98 /* called only inside */
99 static int  DVDChooseAngle( thread_dvd_data_t * );
100 static int  DVDFindCell( thread_dvd_data_t * );
101 static int  DVDFindSector( thread_dvd_data_t * );
102 static int  DVDChapterSelect( thread_dvd_data_t *, int );
103
104 /*****************************************************************************
105  * Functions exported as capabilities. They are declared as static so that
106  * we don't pollute the namespace too much.
107  *****************************************************************************/
108 void _M( input_getfunctions )( function_list_t * p_function_list )
109 {
110 #define input p_function_list->functions.input
111     p_function_list->pf_probe = DVDProbe;
112     input.pf_init             = DVDInit;
113     input.pf_open             = NULL; /* Set in DVDInit */
114     input.pf_close            = NULL;
115     input.pf_end              = DVDEnd;
116     input.pf_read             = DVDRead;
117     input.pf_set_area         = DVDSetArea;
118     input.pf_demux            = input_DemuxPS;
119     input.pf_new_packet       = DVDNewPacket;
120     input.pf_new_pes          = DVDNewPES;
121     input.pf_delete_packet    = DVDDeletePacket;
122     input.pf_delete_pes       = DVDDeletePES;
123     input.pf_rewind           = DVDRewind;
124     input.pf_seek             = DVDSeek;
125 #undef input
126 }
127
128 /*
129  * Data reading functions
130  */
131
132 /*****************************************************************************
133  * DVDProbe: verifies that the stream is a PS stream
134  *****************************************************************************/
135 static int DVDProbe( probedata_t *p_data )
136 {
137     input_thread_t * p_input = (input_thread_t *)p_data;
138
139     char * psz_name = p_input->p_source;
140     int i_handle;
141     int i_score = 5;
142
143     if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
144     {
145 #ifdef HAVE_CSS
146         return( 999 );
147 #else /* HAVE_CSS */
148         return( 998 );
149 #endif /* HAVE_CSS */
150     }
151
152     if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
153     {
154         /* If the user specified "dvd:" then it's probably a DVD */
155 #ifdef HAVE_CSS
156         i_score = 100;
157 #else /* HAVE_CSS */
158         i_score = 90;
159 #endif /* HAVE_CSS */
160         psz_name += 4;
161     }
162
163     i_handle = open( psz_name, 0 );
164     if( i_handle == -1 )
165     {
166         return( 0 );
167     }
168     close( i_handle );
169
170     return( i_score );
171 }
172
173 /*****************************************************************************
174  * DVDInit: initializes DVD structures
175  *****************************************************************************/
176 static void DVDInit( input_thread_t * p_input )
177 {
178     thread_dvd_data_t *  p_dvd;
179     input_area_t *       p_area;
180     int                  i_title;
181     int                  i_chapter;
182     int                  i;
183
184     p_dvd = malloc( sizeof(thread_dvd_data_t) );
185     if( p_dvd == NULL )
186     {
187         intf_ErrMsg( "dvd error: out of memory" );
188         p_input->b_error = 1;
189         return;
190     }
191
192     p_input->p_plugin_data = (void *)p_dvd;
193     p_input->p_method_data = NULL;
194
195     /* Set callback */
196     p_input->pf_open  = p_input->pf_file_open;
197     p_input->pf_close = p_input->pf_file_close;
198
199     p_dvd->i_fd = p_input->i_handle;
200
201     /* reading several block once seems to cause lock-up
202      * when using input_ToggleES
203      * who wrote thez damn buggy piece of shit ??? --stef */
204     p_dvd->i_block_once = 32;
205     p_input->i_read_once = 128;
206
207     i = CSSTest( p_input->i_handle );
208
209     if( i < 0 )
210     {
211         intf_ErrMsg( "dvd error: error in css" );
212         free( p_dvd );
213         p_input->b_error = 1;
214         return;
215     }
216
217     p_dvd->b_encrypted = i;
218
219     lseek( p_input->i_handle, 0, SEEK_SET );
220
221     /* Reading structures initialisation */
222     p_input->p_method_data =
223         DVDNetlistInit( 2048, 4096, 2048, DVD_LB_SIZE, p_dvd->i_block_once );
224     intf_WarnMsg( 2, "dvd info: netlist initialized" );
225
226     /* Ifo allocation & initialisation */
227     if( IfoCreate( p_dvd ) < 0 )
228     {
229         intf_ErrMsg( "dvd error: allcation error in ifo" );
230         p_input->b_error = 1;
231         return;
232     }
233
234     if( IfoInit( p_dvd->p_ifo ) < 0 )
235     {
236         intf_ErrMsg( "dvd error: fatal failure in ifo" );
237         free( p_dvd );
238         p_input->b_error = 1;
239         return;
240     }
241
242     /* CSS initialisation */
243     if( p_dvd->b_encrypted )
244     {
245         p_dvd->p_css = malloc( sizeof(css_t) );
246         if( p_dvd->p_css == NULL )
247         {
248             intf_ErrMsg( "dvd error: couldn't create css structure" );
249             free( p_dvd );
250             p_input->b_error = 1;
251             return;
252         }
253
254         p_dvd->p_css->i_agid = 0;
255
256         if( CSSInit( p_input->i_handle, p_dvd->p_css ) < 0 )
257         {
258             intf_ErrMsg( "dvd error: fatal failure in css" );
259             free( p_dvd->p_css );
260             free( p_dvd );
261             p_input->b_error = 1;
262             return;
263         }
264
265         intf_WarnMsg( 2, "dvd info: css initialized" );
266     }
267
268     /* Set stream and area data */
269     vlc_mutex_lock( &p_input->stream.stream_lock );
270
271     /* Initialize ES structures */
272     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
273
274     /* disc input method */
275     p_input->stream.i_method = INPUT_METHOD_DVD;
276
277 #define title_inf p_dvd->p_ifo->vmg.title_inf
278     intf_WarnMsg( 2, "dvd info: number of titles: %d", title_inf.i_title_nb );
279
280 #define area p_input->stream.pp_areas
281     /* We start from 1 here since the default area 0
282      * is reserved for video_ts.vob */
283     for( i = 1 ; i <= title_inf.i_title_nb ; i++ )
284     {
285         input_AddArea( p_input );
286
287         /* Titles are Program Chains */
288         area[i]->i_id = i;
289
290         /* Absolute start offset and size 
291          * We can only set that with vts ifo, so we do it during the
292          * first call to DVDSetArea */
293         area[i]->i_start = 0;
294         area[i]->i_size = 0;
295
296         /* Number of chapters */
297         area[i]->i_part_nb = title_inf.p_attr[i-1].i_chapter_nb;
298         area[i]->i_part = 1;
299
300         /* Number of angles */
301         area[i]->i_angle_nb = 0;
302         area[i]->i_angle = 1;
303
304         /* Offset to vts_i_0.ifo */
305         area[i]->i_plugin_data = p_dvd->p_ifo->i_off +
306                        ( title_inf.p_attr[i-1].i_start_sector * DVD_LB_SIZE );
307     }   
308 #undef area
309
310     /* Get requested title - if none try the first title */
311     i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
312     if( i_title <= 0 || i_title > title_inf.i_title_nb )
313     {
314         i_title = 1;
315     }
316
317 #undef title_inf
318
319     /* Get requested chapter - if none defaults to first one */
320     i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
321     if( i_chapter <= 0 )
322     {
323         i_chapter = 1;
324     }
325
326     p_input->stream.pp_areas[i_title]->i_part = i_chapter;
327
328     p_area = p_input->stream.pp_areas[i_title];
329
330     /* set title, chapter, audio and subpic */
331     DVDSetArea( p_input, p_area );
332
333     vlc_mutex_unlock( &p_input->stream.stream_lock );
334
335     return;
336 }
337
338 /*****************************************************************************
339  * DVDEnd: frees unused data
340  *****************************************************************************/
341 static void DVDEnd( input_thread_t * p_input )
342 {
343     thread_dvd_data_t *     p_dvd;
344     dvd_netlist_t *         p_netlist;
345
346     p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
347     p_netlist = (dvd_netlist_t *)p_input->p_method_data;
348
349     if( p_dvd->b_encrypted )
350     {
351         free( p_dvd->p_css );
352     }
353
354     IfoDestroy( p_dvd->p_ifo );
355     free( p_dvd );
356     DVDNetlistEnd( p_netlist );
357 }
358
359 /*****************************************************************************
360  * DVDSetArea: initialize input data for title x, chapter y.
361  * It should be called for each user navigation request.
362  *****************************************************************************
363  * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
364  * Note that you have to take the lock before entering here.
365  *****************************************************************************/
366 static int DVDSetArea( input_thread_t * p_input, input_area_t * p_area )
367 {
368     thread_dvd_data_t *  p_dvd;
369     es_descriptor_t *    p_es;
370     u16                  i_id;
371     int                  i_vts_title;
372     int                  i_audio;
373     int                  i_spu;
374     int                  i;
375     int                  j;
376
377     p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
378
379     /* we can't use the interface slider until initilization is complete */
380     p_input->stream.b_seekable = 0;
381
382     if( p_area != p_input->stream.p_selected_area )
383     {
384
385         /*
386          *  We have to load all title information
387          */
388         /* Change the default area */
389         p_input->stream.p_selected_area =
390                     p_input->stream.pp_areas[p_area->i_id];
391
392         /* release the lock to to let the interface go */
393 //        vlc_mutex_unlock( &p_input->stream.stream_lock );
394
395         /* title number: it is not vts nb!,
396          * it is what appears in the interface list */
397         p_dvd->i_title = p_area->i_id;
398         p_dvd->p_ifo->i_title = p_dvd->i_title;
399
400         /* set number of chapters of current title */
401         p_dvd->i_chapter_nb = p_area->i_part_nb;
402
403         /* ifo vts */
404         if( IfoTitleSet( p_dvd->p_ifo ) < 0 )
405         {
406             intf_ErrMsg( "dvd error: fatal error in vts ifo" );
407             free( p_dvd );
408             p_input->b_error = 1;
409             return -1;
410         }
411
412 #define vmg p_dvd->p_ifo->vmg
413 #define vts p_dvd->p_ifo->vts
414         /* title position inside the selected vts */
415         i_vts_title = vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_num;
416         p_dvd->i_title_id =
417             vts.title_inf.p_title_start[i_vts_title-1].i_title_id;
418
419         intf_WarnMsg( 3, "dvd: title %d vts_title %d pgc %d",
420                         p_dvd->i_title,
421                         i_vts_title,
422                         p_dvd->i_title_id );
423
424         /* css title key for current vts */
425         if( p_dvd->b_encrypted )
426         {
427             /* this one is vts number */
428             p_dvd->p_css->i_title =
429                     vmg.title_inf.p_attr[p_dvd->i_title-1].i_title_set_num;
430             p_dvd->p_css->i_title_pos =
431                     vts.i_pos +
432                     vts.manager_inf.i_title_vob_start_sector * DVD_LB_SIZE;
433
434             j = CSSGetKey( p_input->i_handle, p_dvd->p_css );
435             if( j < 0 )
436             {
437                 intf_ErrMsg( "dvd error: fatal error in vts css key" );
438                 free( p_dvd );
439                 p_input->b_error = 1;
440                 return -1;
441             }
442             else if( j > 0 )
443             {
444                 intf_ErrMsg( "dvd error: css decryption unavailable" );
445                 free( p_dvd );
446                 p_input->b_error = 1;
447                 return -1;
448             }
449         }
450
451         /*
452          * Angle management
453          */
454         p_dvd->i_angle_nb = vmg.title_inf.p_attr[p_dvd->i_title-1].i_angle_nb;
455         p_dvd->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
456         if( ( p_dvd->i_angle <= 0 ) || p_dvd->i_angle > p_dvd->i_angle_nb )
457         {
458             p_dvd->i_angle = 1;
459         }
460     
461         /*
462          * Set selected title start and size
463          */
464         
465         /* title set offset */
466         p_dvd->i_title_start = vts.i_pos + DVD_LB_SIZE *
467                       (off_t)( vts.manager_inf.i_title_vob_start_sector );
468
469         /* last video cell */
470         p_dvd->i_cell = 0;
471         p_dvd->i_prg_cell = -1 +
472             vts.title_unit.p_title[p_dvd->i_title_id-1].title.i_cell_nb;
473
474         if( DVDFindCell( p_dvd ) < 0 )
475         {
476             intf_ErrMsg( "dvd error: can't find title end" );
477             p_input->b_error = 1;
478             return -1;
479         }
480
481         /* temporary hack to fix size in some dvds */
482         if( p_dvd->i_cell >= vts.cell_inf.i_cell_nb )
483         {
484             p_dvd->i_cell = vts.cell_inf.i_cell_nb - 1;
485         }
486
487         p_dvd->i_sector = 0;
488         p_dvd->i_size = DVD_LB_SIZE *
489           (off_t)( vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
490         intf_WarnMsg( 2, "dvd info: stream size 1: %lld @ %d", p_dvd->i_size,
491                       vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector );
492
493         if( DVDChapterSelect( p_dvd, 1 ) < 0 )
494         {
495             intf_ErrMsg( "dvd error: can't find first chapter" );
496             p_input->b_error = 1;
497             return -1;
498         }
499
500         p_dvd->i_size -= (off_t)( p_dvd->i_sector + 1 ) *DVD_LB_SIZE;
501
502         IfoPrintTitle( p_dvd );
503
504 //        vlc_mutex_lock( &p_input->stream.stream_lock );
505
506         /* Area definition */
507         p_input->stream.p_selected_area->i_start = p_dvd->i_start;
508         p_input->stream.p_selected_area->i_size = p_dvd->i_size;
509         p_input->stream.p_selected_area->i_angle_nb = p_dvd->i_angle_nb;
510         p_input->stream.p_selected_area->i_angle = p_dvd->i_angle;
511
512         /*
513          * Destroy obsolete ES by reinitializing program 0
514          * and find all ES in title with ifo data
515          */
516         if( p_input->stream.pp_programs != NULL )
517         {
518             /* We don't use input_EndStream here since
519              * we keep area structures */
520
521             for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
522             {
523                 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
524             }
525
526             input_DelProgram( p_input, p_input->stream.pp_programs[0] );
527
528             p_input->stream.pp_selected_es = NULL;
529             p_input->stream.i_selected_es_number = 0;
530         }
531
532         input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
533
534         /* No PSM to read in DVD mode, we already have all information */
535         p_input->stream.pp_programs[0]->b_is_ok = 1;
536         p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_START;
537
538         p_es = NULL;
539
540         /* ES 0 -> video MPEG2 */
541         IfoPrintVideo( p_dvd );
542
543         p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
544         p_es->i_stream_id = 0xe0;
545         p_es->i_type = MPEG2_VIDEO_ES;
546         p_es->i_cat = VIDEO_ES;
547         intf_WarnMsg( 1, "dvd info: video mpeg2 stream" );
548         if( p_main->b_video )
549         {
550             input_SelectES( p_input, p_es );
551         }
552         intf_WarnMsg( 4, "dvd info: video selected" );
553
554 #define audio_status \
555     vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_audio_status[i-1]
556         /* Audio ES, in the order they appear in .ifo */
557         for( i = 1 ; i <= vts.manager_inf.i_audio_nb ; i++ )
558         {
559             IfoPrintAudio( p_dvd, i );
560
561             /* audio channel is active if first byte is 0x80 */
562             if( audio_status.i_available )
563             {
564                 switch( vts.manager_inf.p_audio_attr[i-1].i_coding_mode )
565                 {
566                 case 0x00:              /* AC3 */
567                     i_id = ( ( 0x80 + audio_status.i_position ) << 8 ) | 0xbd;
568                     p_es = input_AddES( p_input,
569                                p_input->stream.pp_programs[0], i_id, 0 );
570                     p_es->i_stream_id = 0xbd;
571                     p_es->i_type = AC3_AUDIO_ES;
572                     p_es->b_audio = 1;
573                     p_es->i_cat = AUDIO_ES;
574                     strcpy( p_es->psz_desc, IfoLanguage( hton16(
575                         vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
576                     strcat( p_es->psz_desc, " (ac3)" );
577     
578                     intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
579                                   i, p_es->psz_desc, i_id );
580     
581                     break;
582                 case 0x02:
583                 case 0x03:              /* MPEG audio */
584                     i_id = 0xc0 + audio_status.i_position;
585                     p_es = input_AddES( p_input,
586                                     p_input->stream.pp_programs[0], i_id, 0 );
587                     p_es->i_stream_id = i_id;
588                     p_es->i_type = MPEG2_AUDIO_ES;
589                     p_es->b_audio = 1;
590                     p_es->i_cat = AUDIO_ES;
591                     strcpy( p_es->psz_desc, IfoLanguage( hton16(
592                         vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
593                     strcat( p_es->psz_desc, " (mpeg)" );
594     
595                     intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
596                                   i, p_es->psz_desc, i_id );
597     
598                     break;
599                 case 0x04:              /* LPCM */
600     
601                     i_id = ( ( 0xa0 + audio_status.i_position ) << 8 ) | 0xbd;
602                     p_es = input_AddES( p_input,
603                                     p_input->stream.pp_programs[0], i_id, 0 );
604                     p_es->i_stream_id = i_id;
605                     p_es->i_type = LPCM_AUDIO_ES;
606                     p_es->b_audio = 1;
607                     p_es->i_cat = AUDIO_ES;
608                     strcpy( p_es->psz_desc, IfoLanguage( hton16(
609                         vts.manager_inf.p_audio_attr[i-1].i_lang_code ) ) ); 
610                     strcat( p_es->psz_desc, " (lpcm)" );
611     
612                     intf_WarnMsg( 3, "dvd info: audio stream %d %s\t(0x%x)",
613                                   i, p_es->psz_desc, i_id );
614                     break;
615                 case 0x06:              /* DTS */
616                     i_id = ( ( 0x88 + audio_status.i_position ) << 8 ) | 0xbd;
617                     intf_ErrMsg( "dvd warning: DTS audio not handled yet"
618                                  "(0x%x)", i_id );
619                     break;
620                 default:
621                     i_id = 0;
622                     intf_ErrMsg( "dvd warning: unknown audio type %.2x",
623                              vts.manager_inf.p_audio_attr[i-1].i_coding_mode );
624                 }
625             }
626         }
627 #undef audio_status
628 #define spu_status \
629     vts.title_unit.p_title[p_dvd->i_title_id-1].title.pi_spu_status[i-1]
630
631         /* Sub Picture ES */
632            
633         for( i = 1 ; i <= vts.manager_inf.i_spu_nb; i++ )
634         {
635             IfoPrintSpu( p_dvd, i );
636
637             if( spu_status.i_available )
638             {
639                 /*  there are several streams for one spu */
640                 if(  vts.manager_inf.video_attr.i_ratio )
641                 {
642                     /* 16:9 */
643                     switch( vts.manager_inf.video_attr.i_perm_displ )
644                     {
645                     case 1:
646                         i_id = ( ( 0x20 + spu_status.i_position_pan ) << 8 )
647                                | 0xbd;
648                         break;
649                     case 2:
650                         i_id = ( ( 0x20 + spu_status.i_position_letter ) << 8 )
651                                | 0xbd;
652                         break;
653                     default:
654                         i_id = ( ( 0x20 + spu_status.i_position_wide ) << 8 )
655                                | 0xbd;
656                         break;
657                     }
658                 }
659                 else
660                 {
661                     /* 4:3 */
662                     i_id = ( ( 0x20 + spu_status.i_position_43 ) << 8 )
663                            | 0xbd;
664                 }
665                 p_es = input_AddES( p_input,
666                                     p_input->stream.pp_programs[0], i_id, 0 );
667                 p_es->i_stream_id = 0xbd;
668                 p_es->i_type = DVD_SPU_ES;
669                 p_es->i_cat = SPU_ES;
670                 strcpy( p_es->psz_desc, IfoLanguage( hton16(
671                     vts.manager_inf.p_spu_attr[i-1].i_lang_code ) ) ); 
672                 intf_WarnMsg( 3, "dvd info: spu stream %d %s\t(0x%x)",
673                               i, p_es->psz_desc, i_id );
674             }
675         }
676 #undef spu_status
677         if( p_main->b_audio )
678         {
679             /* For audio: first one if none or a not existing one specified */
680             i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
681             if( i_audio < 0 || i_audio > vts.manager_inf.i_audio_nb )
682             {
683                 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
684                 i_audio = 1;
685             }
686             if( i_audio > 0 && vts.manager_inf.i_audio_nb > 0 )
687             {
688                 input_SelectES( p_input, p_input->stream.pp_es[i_audio] );
689             }
690         }
691
692         if( p_main->b_video )
693         {
694             /* for spu, default is none */
695             i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
696             if( i_spu < 0 || i_spu > vts.manager_inf.i_spu_nb )
697             {
698                 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
699                 i_spu = 0;
700             }
701             if( i_spu > 0 && vts.manager_inf.i_spu_nb > 0 )
702             {
703                 i_spu += vts.manager_inf.i_audio_nb;
704                 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
705             }
706         }
707     } /* i_title >= 0 */
708     else
709     {
710         p_area = p_input->stream.p_selected_area;
711     }
712 #undef vts
713 #undef vmg
714
715     /*
716      * Chapter selection
717      */
718
719     
720     if( p_area->i_part != p_dvd->i_chapter )
721     {
722         if( ( p_area->i_part > 0 ) &&
723             ( p_area->i_part <= p_area->i_part_nb ))
724         {
725             if( DVDChapterSelect( p_dvd, p_area->i_part ) < 0 )
726             {
727                 intf_ErrMsg( "dvd error: can't set chapter in area" );
728                 p_input->b_error = 1;
729                 return -1;
730             }
731     
732             p_input->stream.p_selected_area->i_tell = p_dvd->i_start -
733                                                       p_area->i_start;
734             p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
735     
736             intf_WarnMsg( 2, "dvd info: chapter %d start at: %lld",
737                                         p_area->i_part, p_area->i_tell );
738         }
739         else
740         {
741             p_area->i_part = 1;
742             p_dvd->i_chapter = 1;
743         }
744     }
745
746 #define title \
747     p_dvd->p_ifo->vts.title_unit.p_title[p_dvd->i_title_id-1].title
748     if( p_area->i_angle != p_dvd->i_angle )
749     {
750         if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
751         {
752             if( ( p_area->i_angle - p_dvd->i_angle ) < 0 )
753             {
754                 p_dvd->i_cell = 0;
755             }
756             p_dvd->i_prg_cell += ( p_area->i_angle - p_dvd->i_angle );
757             p_dvd->i_angle = p_area->i_angle;
758     
759             DVDFindSector( p_dvd );
760             p_dvd->i_cell += p_dvd->i_angle_cell;
761         }
762         else
763         {
764             p_dvd->i_angle = p_area->i_angle;
765         }
766
767         intf_WarnMsg( 2, "dvd info: angle %d selected", p_area->i_angle );
768     }
769
770     /* warn interface that something has changed */
771     p_input->stream.b_seekable = 1;
772     p_input->stream.b_changed = 1;
773
774     p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_REINIT;
775
776     return 0;
777 }
778
779
780 /*****************************************************************************
781  * DVDRead: reads data packets into the netlist.
782  *****************************************************************************
783  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
784  * EOF.
785  *****************************************************************************/
786 static int DVDRead( input_thread_t * p_input,
787                     data_packet_t ** pp_packets )
788 {
789     thread_dvd_data_t *     p_dvd;
790     dvd_netlist_t *         p_netlist;
791     struct iovec *          p_vec;
792     struct data_packet_s *  pp_data[p_input->i_read_once];
793     u8 *                    pi_cur;
794     int                     i_block_once;
795     int                     i_packet_size;
796     int                     i_iovec;
797     int                     i_packet;
798     int                     i_pos;
799     int                     i_read_bytes;
800     int                     i_read_blocks;
801     off_t                   i_off;
802     boolean_t               b_eof;
803     boolean_t               b_eot;
804
805     p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
806     p_netlist = (dvd_netlist_t *)p_input->p_method_data;
807
808     /* Get an iovec pointer */
809     if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
810     {
811         intf_ErrMsg( "dvd error: can't get iovec" );
812         return -1;
813     }
814
815     i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
816
817     /* Get the position of the next cell if we're at cell end */
818     if( i_block_once <= 0 )
819     {
820         int     i_angle;
821
822         p_dvd->i_cell++;
823         p_dvd->i_angle_cell++;
824
825         /* Find cell index in adress map */
826         if( DVDFindSector( p_dvd ) < 0 )
827         {
828             pp_packets[0] = NULL;
829             intf_ErrMsg( "dvd error: can't find next cell" );
830             return 1;
831         }
832
833         /* Position the fd pointer on the right address */
834         i_off = lseek( p_dvd->i_fd,
835                        p_dvd->i_title_start +
836                        (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET );
837
838         /* update chapter : it will be easier when we have navigation
839          * ES support */
840         if( p_dvd->i_chapter < ( p_dvd->i_chapter_nb - 1 ) )
841         {
842             if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
843             {
844                 i_angle = p_dvd->i_angle - 1;
845             }
846             else
847             {
848                 i_angle = 0;
849             }
850             if( title.chapter_map.pi_start_cell[p_dvd->i_chapter] <=
851                 ( p_dvd->i_prg_cell - i_angle + 1 ) )
852             {
853                 p_dvd->i_chapter++;
854             }
855         }
856
857         vlc_mutex_lock( &p_input->stream.stream_lock );
858
859         p_input->stream.p_selected_area->i_tell = i_off -
860                                     p_input->stream.p_selected_area->i_start;
861         p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
862
863         /* the synchro has to be reinitialized when we change cell */
864         p_input->stream.pp_programs[0]->i_synchro_state = SYNCHRO_REINIT;
865
866         vlc_mutex_unlock( &p_input->stream.stream_lock );
867
868         i_block_once = p_dvd->i_end_sector - p_dvd->i_sector + 1;
869     }
870
871     /* the number of blocks read is the maw between the requested
872      * value and the leaving block in the cell */
873     if( i_block_once > p_dvd->i_block_once )
874     {
875         i_block_once = p_dvd->i_block_once;
876     }
877 //intf_WarnMsg( 2, "Sector: 0x%x Read: %d Chapter: %d", p_dvd->i_sector, i_block_once, p_dvd->i_chapter );
878
879     p_netlist->i_read_once = i_block_once;
880
881     /* Reads from DVD */
882     i_read_bytes = readv( p_dvd->i_fd, p_vec, i_block_once );
883     i_read_blocks = ( i_read_bytes + 0x7ff ) >> 11;
884
885     /* Update netlist indexes */
886     DVDMviovec( p_netlist, i_read_blocks, pp_data );
887
888     /* Update global position */
889     p_dvd->i_sector += i_read_blocks;
890
891     i_packet = 0;
892
893     /* Read headers to compute payload length */
894     for( i_iovec = 0 ; i_iovec < i_read_blocks ; i_iovec++ )
895     {
896         if( p_dvd->b_encrypted )
897         {
898             CSSDescrambleSector( p_dvd->p_css->pi_title_key, 
899                                  p_vec[i_iovec].iov_base );
900             ((u8*)(p_vec[i_iovec].iov_base))[0x14] &= 0x8F;
901         }
902
903         i_pos = 0;
904
905         while( i_pos < p_netlist->i_buffer_size )
906         {
907             pi_cur = (u8*)(p_vec[i_iovec].iov_base + i_pos);
908
909             /*default header */
910             if( U32_AT( pi_cur ) != 0x1BA )
911             {
912                 /* That's the case for all packets, except pack header. */
913                 i_packet_size = U16_AT( pi_cur + 4 );
914                 pp_packets[i_packet] = DVDNewPtr( p_netlist );
915             }
916             else
917             {
918                 /* Pack header. */
919                 if( ( pi_cur[4] & 0xC0 ) == 0x40 )
920                 {
921                     /* MPEG-2 */
922                     i_packet_size = 8;
923                 }
924                 else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
925                 {
926                     /* MPEG-1 */
927                     i_packet_size = 6;
928                 }
929                 else
930                 {
931                     intf_ErrMsg( "Unable to determine stream type" );
932                     return( -1 );
933                 }
934
935                 pp_packets[i_packet] = pp_data[i_iovec];
936
937             }
938
939             (*pp_data[i_iovec]->pi_refcount)++;
940
941             pp_packets[i_packet]->pi_refcount = pp_data[i_iovec]->pi_refcount;
942
943             pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
944
945             pp_packets[i_packet]->p_payload_start =
946                     pp_packets[i_packet]->p_buffer + i_pos;
947
948             pp_packets[i_packet]->p_payload_end =
949                     pp_packets[i_packet]->p_payload_start + i_packet_size + 6;
950
951             pp_packets[i_packet]->p_next = NULL;
952             pp_packets[i_packet]->b_discard_payload = 0;
953
954             i_packet++;
955             i_pos += i_packet_size + 6;
956         }
957     }
958
959     pp_packets[i_packet] = NULL;
960
961     vlc_mutex_lock( &p_input->stream.stream_lock );
962
963     p_input->stream.p_selected_area->i_tell += i_read_bytes;
964     b_eot = !( p_input->stream.p_selected_area->i_tell < p_dvd->i_size );
965     b_eof = b_eot && ( ( p_dvd->i_title + 1 ) >= p_input->stream.i_area_nb );
966
967     vlc_mutex_unlock( &p_input->stream.stream_lock );
968
969     if( b_eof )
970     {
971         return 1;
972     }
973
974     if( b_eot )
975     {
976         intf_WarnMsg( 4, "dvd info: new title" );
977         p_dvd->i_title++;
978         vlc_mutex_lock( &p_input->stream.stream_lock );
979         DVDSetArea( p_input, p_input->stream.pp_areas[p_dvd->i_title] );
980         vlc_mutex_unlock( &p_input->stream.stream_lock );
981         return 0;
982     }
983
984     if( i_read_blocks == i_block_once )
985     {
986         return 0;
987     }
988
989     return -1;
990 }
991
992 /*****************************************************************************
993  * DVDRewind : reads a stream backward
994  *****************************************************************************/
995 static int DVDRewind( input_thread_t * p_input )
996 {
997     return( -1 );
998 }
999
1000 /*****************************************************************************
1001  * DVDSeek : Goes to a given position on the stream.
1002  *****************************************************************************
1003  * This one is used by the input and translate chronological position from
1004  * input to logical position on the device.
1005  * The lock should be taken before calling this function.
1006  *****************************************************************************/
1007 static void DVDSeek( input_thread_t * p_input, off_t i_off )
1008 {
1009     thread_dvd_data_t *     p_dvd;
1010     off_t                   i_pos;
1011     int                     i_prg_cell;
1012     int                     i_cell;
1013     int                     i_chapter;
1014     int                     i_angle;
1015     
1016     p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
1017
1018     /* we have to take care of offset of beginning of title */
1019     i_pos = i_off + p_input->stream.p_selected_area->i_start
1020                   - p_dvd->i_title_start;
1021
1022     /* update navigation data */
1023     p_dvd->i_sector = i_pos >> 11;
1024
1025     i_prg_cell = 0;
1026     i_chapter = 0;
1027
1028     /* parse vobu address map to find program cell */
1029     while( title.p_cell_play[i_prg_cell].i_end_sector < p_dvd->i_sector  )
1030     {
1031         i_prg_cell++;
1032     }
1033
1034     p_dvd->i_prg_cell = i_prg_cell;
1035
1036     if( DVDChooseAngle( p_dvd ) < 0 )
1037     {
1038         p_input->b_error = 1;
1039         return;        
1040     }
1041
1042     p_dvd->i_cell = 0;
1043
1044     /* Find first title cell which is inside program cell */
1045     if( DVDFindCell( p_dvd ) < 0 )
1046     {
1047         /* no following cell : we're at eof */
1048         intf_ErrMsg( "dvd error: cell seeking failed" );
1049         p_input->b_error = 1;
1050         return;
1051     }
1052
1053     i_cell = p_dvd->i_cell;
1054
1055 #define cell p_dvd->p_ifo->vts.cell_inf.p_cell_map[i_cell]
1056     /* parse cell address map to find title cell containing sector */
1057     while( cell.i_end_sector < p_dvd->i_sector )
1058     {
1059         i_cell++;
1060     }
1061
1062     p_dvd->i_cell = i_cell;
1063
1064     /* if we're inside a multi-angle zone, we have to choose i_sector
1065      * in the current angle ; we can't do it all the time since cells
1066      * can be very wide out of such zones */
1067     if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1068     {
1069         p_dvd->i_sector = MAX(
1070                 cell.i_start_sector,
1071                 title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1072     }
1073
1074     p_dvd->i_end_sector = MIN(
1075             cell.i_end_sector,
1076             title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1077 #undef cell
1078     /* update chapter */
1079     if( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1080     {
1081         i_angle = p_dvd->i_angle - 1;
1082     }
1083     else
1084     {
1085         i_angle = 0;
1086     }
1087     while( ( title.chapter_map.pi_start_cell[i_chapter] <=
1088                 ( p_dvd->i_prg_cell - i_angle + 1 ) ) &&
1089            ( i_chapter < ( p_dvd->i_chapter_nb - 1 ) ) )
1090     {
1091         i_chapter++;
1092     }
1093
1094     p_dvd->i_chapter = i_chapter;
1095     p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1096
1097     p_input->stream.p_selected_area->i_tell =
1098                 lseek( p_dvd->i_fd, p_dvd->i_title_start +
1099                        (off_t)( p_dvd->i_sector ) *DVD_LB_SIZE, SEEK_SET ) -
1100                 p_input->stream.p_selected_area->i_start;
1101 /*
1102     intf_WarnMsg( 3, "Program Cell: %d Cell: %d Chapter: %d",
1103                      p_dvd->i_prg_cell, p_dvd->i_cell, p_dvd->i_chapter );
1104 */
1105
1106     return;
1107 }
1108
1109 #define cell  p_dvd->p_ifo->vts.cell_inf
1110
1111 /*****************************************************************************
1112  * DVDFindCell: adjust the title cell index with the program cell
1113  *****************************************************************************/
1114 static int DVDFindCell( thread_dvd_data_t * p_dvd )
1115 {
1116     int                 i_cell;
1117     int                 i_index;
1118
1119     i_cell = p_dvd->i_cell;
1120     i_index = p_dvd->i_prg_cell;
1121
1122     if( i_cell >= cell.i_cell_nb )
1123     {
1124         return -1;
1125     }
1126
1127     while( ( ( title.p_cell_pos[i_index].i_vob_id !=
1128                    cell.p_cell_map[i_cell].i_vob_id ) ||
1129       ( title.p_cell_pos[i_index].i_cell_id !=
1130                    cell.p_cell_map[i_cell].i_cell_id ) ) &&
1131            ( i_cell < cell.i_cell_nb - 1 ) )
1132     {
1133         i_cell++;
1134     }
1135
1136 /*
1137 intf_WarnMsg( 3, "FindCell: i_cell %d i_index %d found %d nb %d",
1138                     p_dvd->i_cell,
1139                     p_dvd->i_prg_cell,
1140                     i_cell,
1141                     cell.i_cell_nb );
1142 */
1143
1144     p_dvd->i_cell = i_cell;
1145
1146     return 0;    
1147 }
1148
1149 #undef cell
1150
1151 /*****************************************************************************
1152  * DVDFindSector: find cell index in adress map from index in
1153  * information table program map and give corresponding sectors.
1154  *****************************************************************************/
1155 static int DVDFindSector( thread_dvd_data_t * p_dvd )
1156 {
1157
1158     if( p_dvd->i_sector > title.p_cell_play[p_dvd->i_prg_cell].i_end_sector )
1159     {
1160         p_dvd->i_prg_cell++;
1161
1162         if( DVDChooseAngle( p_dvd ) < 0 )
1163         {
1164             return -1;
1165         }
1166     }
1167
1168     if( DVDFindCell( p_dvd ) < 0 )
1169     {
1170         intf_ErrMsg( "dvd error: can't find sector" );
1171         return -1;
1172     }
1173
1174     /* Find start and end sectors of new cell */
1175 #if 1
1176     p_dvd->i_sector = MAX(
1177          p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1178          title.p_cell_play[p_dvd->i_prg_cell].i_start_sector );
1179     p_dvd->i_end_sector = MIN(
1180          p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1181          title.p_cell_play[p_dvd->i_prg_cell].i_end_sector );
1182 #else
1183     p_dvd->i_sector = title.p_cell_play[p_dvd->i_prg_cell].i_start_sector;
1184     p_dvd->i_end_sector = title.p_cell_play[p_dvd->i_prg_cell].i_end_sector;
1185 #endif
1186
1187 /*
1188     intf_WarnMsg( 3, "cell: %d sector1: 0x%x end1: 0x%x\n"
1189                    "index: %d sector2: 0x%x end2: 0x%x\n"
1190                    "category: 0x%x ilvu end: 0x%x vobu start 0x%x", 
1191         p_dvd->i_cell,
1192         p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_start_sector,
1193         p_dvd->p_ifo->vts.cell_inf.p_cell_map[p_dvd->i_cell].i_end_sector,
1194         p_dvd->i_prg_cell,
1195         title.p_cell_play[p_dvd->i_prg_cell].i_start_sector,
1196         title.p_cell_play[p_dvd->i_prg_cell].i_end_sector,
1197         title.p_cell_play[p_dvd->i_prg_cell].i_category, 
1198         title.p_cell_play[p_dvd->i_prg_cell].i_first_ilvu_vobu_esector,
1199         title.p_cell_play[p_dvd->i_prg_cell].i_last_vobu_start_sector );
1200 */
1201
1202     return 0;
1203 }
1204
1205 /*****************************************************************************
1206  * DVDChapterSelect: find the cell corresponding to requested chapter
1207  *****************************************************************************/
1208 static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
1209 {
1210
1211     /* Find cell index in Program chain for current chapter */
1212     p_dvd->i_prg_cell = title.chapter_map.pi_start_cell[i_chapter-1] - 1;
1213     p_dvd->i_cell = 0;
1214     p_dvd->i_sector = 0;
1215
1216     DVDChooseAngle( p_dvd );
1217
1218     /* Search for cell_index in cell adress_table and initialize
1219      * start sector */
1220     if( DVDFindSector( p_dvd ) < 0 )
1221     {
1222         intf_ErrMsg( "dvd error: can't select chapter" );
1223         return -1;
1224     }
1225
1226     /* start is : beginning of vts vobs + offset to vob x */
1227     p_dvd->i_start = p_dvd->i_title_start +
1228                          DVD_LB_SIZE * (off_t)( p_dvd->i_sector );
1229
1230     /* Position the fd pointer on the right address */
1231     p_dvd->i_start = lseek( p_dvd->i_fd, p_dvd->i_start, SEEK_SET );
1232
1233     p_dvd->i_chapter = i_chapter;
1234     return 0;
1235 }
1236
1237 /*****************************************************************************
1238  * DVDChooseAngle: select the cell corresponding to the selected angle
1239  *****************************************************************************/
1240 static int DVDChooseAngle( thread_dvd_data_t * p_dvd )
1241 {
1242     /* basic handling of angles */
1243     switch( ( ( title.p_cell_play[p_dvd->i_prg_cell].i_category & 0xf000 )
1244                     >> 12 ) )
1245     {
1246         /* we enter a muli-angle section */
1247         case 0x5:
1248             p_dvd->i_prg_cell += p_dvd->i_angle - 1;
1249             p_dvd->i_angle_cell = 0;
1250             break;
1251         /* we exit a multi-angle section */
1252         case 0x9:
1253         case 0xd:
1254             p_dvd->i_prg_cell += p_dvd->i_angle_nb - p_dvd->i_angle;
1255             break;
1256     }
1257
1258     return 0;
1259 }
1260
1261 #undef title