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