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