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