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