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