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