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