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