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