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