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