]> git.sesse.net Git - vlc/blob - plugins/dvdread/input_dvdread.c
* Fixed a total breakage of decoder plugins introduced by fast_memcpy.
[vlc] / plugins / dvdread / input_dvdread.c
1 /*****************************************************************************
2  * input_dvdread.c: DvdRead 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: libdvdread for ifo files and block reading.
7  *****************************************************************************
8  * Copyright (C) 2001 VideoLAN
9  * $Id: input_dvdread.c,v 1.3 2001/12/07 18:33:07 sam Exp $
10  *
11  * Author: Stéphane Borel <stef@via.ecp.fr>
12  *
13  * Some code taken form the play_title.c by Billy Biggs <vektor@dumbterm.net>
14  * in libdvdread.
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  * 
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
29  *****************************************************************************/
30
31 #define MODULE_NAME dvdread
32 #include "modules_inner.h"
33
34 /*****************************************************************************
35  * Preamble
36  *****************************************************************************/
37 #include "defs.h"
38
39 #include <stdio.h>
40 #include <stdlib.h>
41
42 #ifdef HAVE_UNISTD_H
43 #   include <unistd.h>
44 #endif
45
46 #include <fcntl.h>
47 #include <sys/types.h>
48 #include <string.h>
49 #include <errno.h>
50 #include <assert.h>
51
52 #ifdef STRNCASECMP_IN_STRINGS_H
53 #   include <strings.h>
54 #endif
55
56 #if defined( WIN32 )
57 #   include <io.h>                                                 /* read() */
58 #else
59 #   include <sys/uio.h>                                      /* struct iovec */
60 #endif
61
62
63 #include "common.h"
64 #include "intf_msg.h"
65 #include "threads.h"
66 #include "mtime.h"
67 #include "iso_lang.h"
68 #include "tests.h"
69
70 #if defined( WIN32 )
71 #   include "input_iovec.h"
72 #endif
73
74 #include "stream_control.h"
75 #include "input_ext-intf.h"
76 #include "input_ext-dec.h"
77 #include "input_ext-plugins.h"
78
79 #include "input_dvdread.h"
80
81 #include "debug.h"
82
83 #include "modules.h"
84 #include "modules_export.h"
85
86 /* how many blocks DVDRead will read in each loop */
87 #define DVD_BLOCK_READ_ONCE 64
88 #define DVD_DATA_READ_ONCE  (4 * DVD_BLOCK_READ_ONCE)
89
90 /* Size of netlist */
91 #define DVD_NETLIST_SIZE    512
92
93 /*****************************************************************************
94  * Local prototypes
95  *****************************************************************************/
96 /* called from outside */
97 static int  DvdReadProbe    ( probedata_t *p_data );
98 static void DvdReadInit     ( struct input_thread_s * );
99 static void DvdReadEnd      ( struct input_thread_s * );
100 static void DvdReadOpen     ( struct input_thread_s * );
101 static void DvdReadClose    ( struct input_thread_s * );
102 static int  DvdReadSetArea  ( struct input_thread_s *, struct input_area_s * );
103 static int  DvdReadRead     ( struct input_thread_s *, data_packet_t ** );
104 static void DvdReadSeek     ( struct input_thread_s *, off_t );
105 static int  DvdReadRewind   ( struct input_thread_s * );
106
107 /* called only from here */
108 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data );
109 static void DvdReadFindCell ( thread_dvd_data_t * p_dvd );
110
111 /*****************************************************************************
112  * Functions exported as capabilities. They are declared as static so that
113  * we don't pollute the namespace too much.
114  *****************************************************************************/
115 void _M( input_getfunctions )( function_list_t * p_function_list )
116 {
117 #define input p_function_list->functions.input
118     p_function_list->pf_probe = DvdReadProbe;
119     input.pf_init             = DvdReadInit;
120     input.pf_open             = DvdReadOpen;
121     input.pf_close            = DvdReadClose;
122     input.pf_end              = DvdReadEnd;
123     input.pf_init_bit_stream  = InitBitstream;
124     input.pf_read             = DvdReadRead;
125     input.pf_set_area         = DvdReadSetArea;
126     input.pf_demux            = input_DemuxPS;
127     input.pf_new_packet       = input_NetlistNewPacket;
128     input.pf_new_pes          = input_NetlistNewPES;
129     input.pf_delete_packet    = input_NetlistDeletePacket;
130     input.pf_delete_pes       = input_NetlistDeletePES;
131     input.pf_rewind           = DvdReadRewind;
132     input.pf_seek             = DvdReadSeek;
133 #undef input
134 }
135
136 /*
137  * Data reading functions
138  */
139
140 /*****************************************************************************
141  * DvdReadProbe: verifies that the stream is a PS stream
142  *****************************************************************************/
143 static int DvdReadProbe( probedata_t *p_data )
144 {
145     input_thread_t * p_input = (input_thread_t *)p_data;
146
147     char * psz_name = p_input->p_source;
148     int i_score = 5;
149
150     if( TestMethod( INPUT_METHOD_VAR, "dvdread" ) )
151     {
152         return( 999 );
153     }
154
155     if( ( strlen(psz_name) > 8 ) && !strncasecmp( psz_name, "dvdread:", 8 ) )
156     {
157         /* If the user specified "dvdread:" then he probably wants
158          * to use libdvdread */
159         i_score = 100;
160         psz_name += 4;
161     }
162
163     return( i_score );
164 }
165
166 /*****************************************************************************
167  * DvdReadInit: initializes DVD structures
168  *****************************************************************************/
169 static void DvdReadInit( input_thread_t * p_input )
170 {
171     thread_dvd_data_t *  p_dvd;
172     input_area_t *       p_area;
173     int                  i_title;
174     int                  i_chapter;
175     int                  i;
176
177     p_dvd = malloc( sizeof(thread_dvd_data_t) );
178     if( p_dvd == NULL )
179     {
180         intf_ErrMsg( "dvdread error: out of memory" );
181         p_input->b_error = 1;
182         return;
183     }
184
185     /* we take the pointer to dvd_reader_t back  */
186     p_dvd->p_dvdread = (dvd_reader_t *)p_input->p_plugin_data;
187     p_dvd->p_title = NULL;
188     p_dvd->p_vts_file = NULL;
189
190     p_input->p_plugin_data = (void *)p_dvd;
191     p_input->p_method_data = NULL;
192
193     /* We read DVD_BLOCK_READ_ONCE in each loop, so the input will receive
194      * DVD_DATA_READ_ONCE at most */
195     p_dvd->i_block_once = DVD_BLOCK_READ_ONCE;
196     /* this value mustn't be modifed */
197     p_input->i_read_once = DVD_DATA_READ_ONCE;
198
199     /* Reading structures initialisation */
200     input_NetlistInit( p_input,  DVD_NETLIST_SIZE, 2 * DVD_NETLIST_SIZE,
201                    DVD_NETLIST_SIZE, DVD_VIDEO_LB_LEN, p_dvd->i_block_once );
202     intf_WarnMsg( 2, "dvdread info: netlist initialized" );
203
204     /* Ifo allocation & initialisation */
205     if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
206     {
207         intf_ErrMsg( "dvdread error: can't open VMG info" );
208         DVDClose( p_dvd->p_dvdread );
209         free( p_dvd );
210         p_input->b_error = 1;
211         return;
212     }
213     intf_WarnMsg( 2, "dvdread info: VMG opened" );
214
215     /* Set stream and area data */
216     vlc_mutex_lock( &p_input->stream.stream_lock );
217
218     /* Initialize ES structures */
219     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
220
221     /* disc input method */
222     p_input->stream.i_method = INPUT_METHOD_DVD;
223
224 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
225     intf_WarnMsg( 2, "dvdread info: number of titles: %d", tt_srpt->nr_of_srpts );
226
227 #define area p_input->stream.pp_areas
228     /* We start from 1 here since the default area 0
229      * is reserved for video_ts.vob */
230     for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
231     {
232         input_AddArea( p_input );
233
234         /* Titles are Program Chains */
235         area[i]->i_id = i;
236
237         /* Absolute start offset and size
238          * We can only set that with vts ifo, so we do it during the
239          * first call to DVDSetArea */
240         area[i]->i_start = 0;
241         area[i]->i_size = 0;
242
243         /* Number of chapters */
244         area[i]->i_part_nb = tt_srpt->title[i-1].nr_of_ptts;
245         area[i]->i_part = 1;
246
247         /* Number of angles */
248         area[i]->i_angle_nb = 0;
249         area[i]->i_angle = 1;
250
251         area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
252     }
253 #undef area
254
255     /* Get requested title - if none try the first title */
256     i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
257     if( i_title <= 0 || i_title > tt_srpt->nr_of_srpts )
258     {
259         i_title = 1;
260     }
261
262 #undef tt_srpt
263
264     /* Get requested chapter - if none defaults to first one */
265     i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
266     if( i_chapter <= 0 )
267     {
268         i_chapter = 1;
269     }
270
271     p_input->stream.pp_areas[i_title]->i_part = i_chapter;
272
273     p_area = p_input->stream.pp_areas[i_title];
274
275     /* set title, chapter, audio and subpic */
276     DvdReadSetArea( p_input, p_area );
277
278     vlc_mutex_unlock( &p_input->stream.stream_lock );
279
280     return;
281 }
282
283 /*****************************************************************************
284  * DvdReadOpen: open libdvdread
285  *****************************************************************************/
286 static void DvdReadOpen( struct input_thread_s *p_input )
287 {
288     dvd_reader_t    *p_dvdread;
289
290     vlc_mutex_lock( &p_input->stream.stream_lock );
291
292     /* If we are here we can control the pace... */
293     p_input->stream.b_pace_control = 1;
294
295     p_input->stream.b_seekable = 1;
296     p_input->stream.p_selected_area->i_size = 0;
297
298     p_input->stream.p_selected_area->i_tell = 0;
299
300     vlc_mutex_unlock( &p_input->stream.stream_lock );
301
302     /* XXX: put this shit in an access plugin */
303     if( strlen( p_input->p_source ) > 8
304          && !strncasecmp( p_input->p_source, "dvdread:", 8 ) )
305     {
306         p_dvdread = DVDOpen( p_input->p_source + 8 );
307     }
308     else
309     {
310         p_dvdread = DVDOpen( p_input->p_source );
311     }
312
313     if( ! p_dvdread )
314     {
315         p_input->b_error = 1;
316         return;
317     }
318
319     /* the vlc input doesn't handle the file descriptor with libdvdread */
320     p_input->i_handle = -1;
321
322     /* p_dvdread is stored in p_plugin_data until we have
323      * malloc'ed a dvd_thread_data_t */
324     p_input->p_plugin_data = (void *) p_dvdread;
325 }
326
327 /*****************************************************************************
328  * DvdReadClose: close libdvdread
329  *****************************************************************************/
330 static void DvdReadClose( struct input_thread_s *p_input )
331 {
332     DVDClose( ((thread_dvd_data_t*)p_input->p_plugin_data)->p_dvdread );
333     free( p_input->p_plugin_data );
334     p_input->p_plugin_data = NULL;
335
336 }
337
338 /*****************************************************************************
339  * DvdReadEnd: frees unused data
340  *****************************************************************************/
341 static void DvdReadEnd( input_thread_t * p_input )
342 {
343     thread_dvd_data_t *     p_dvd;
344
345     p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
346
347     /* close libdvdread */
348     DVDCloseFile( p_dvd->p_title );
349     ifoClose( p_dvd->p_vts_file );
350     ifoClose( p_dvd->p_vmg_file );
351
352     /* Close netlist */
353     input_NetlistEnd( p_input );
354     p_input->p_method_data = NULL;
355 }
356
357 #define p_pgc         p_dvd->p_cur_pgc
358
359 /*****************************************************************************
360  * DvdReadSetArea: initialize input data for title x, chapter y.
361  * It should be called for each user navigation request.
362  *****************************************************************************
363  * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
364  * Note that you have to take the lock before entering here.
365  *****************************************************************************/
366 static int DvdReadSetArea( input_thread_t * p_input, input_area_t * p_area )
367 {
368     thread_dvd_data_t *  p_dvd;
369     int                  pgc_id = 0;
370     int                  pgn = 0;
371
372     p_dvd = (thread_dvd_data_t*)p_input->p_plugin_data;
373
374     /* we can't use the interface slider until initilization is complete */
375     p_input->stream.b_seekable = 0;
376
377     if( p_area != p_input->stream.p_selected_area )
378     {
379         es_descriptor_t *    p_es;
380         int                  i_cell = 0;
381         int                  i_audio_nb = 0;
382         int                  i_spu_nb = 0;
383         int                  i;
384
385 #define p_vmg         p_dvd->p_vmg_file
386 #define p_vts         p_dvd->p_vts_file
387         if( p_dvd->p_title != NULL )
388         {
389             DVDCloseFile( p_dvd->p_title );
390         }
391
392         if( p_vts != NULL )
393         {
394             ifoClose( p_vts );
395         }
396
397         /* Reset the Chapter position of the old title */
398         p_input->stream.p_selected_area->i_part = 1;
399
400         /*
401          *  We have to load all title information
402          */
403         /* Change the default area */
404         p_input->stream.p_selected_area = p_area;
405
406         intf_WarnMsg( 12, "dvdread: open VTS %d, for title %d",
407             p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
408             p_area->i_id );
409
410         /* ifo vts */
411         if( ! ( p_vts = ifoOpen( p_dvd->p_dvdread,
412                 p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr ) ) )
413         {
414             intf_ErrMsg( "dvdread error: fatal error in vts ifo" );
415             ifoClose( p_vmg );
416             DVDClose( p_dvd->p_dvdread );
417             p_input->b_error = 1;
418             return -1;
419         }
420
421         /* title position inside the selected vts */
422         p_dvd->i_ttn = p_vmg->tt_srpt->title[ p_area->i_id - 1 ].vts_ttn;
423
424         /*
425          * Set selected title start
426          */
427         pgc_id = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgcn;
428         pgn = p_vts->vts_ptt_srpt->title[p_dvd->i_ttn-1].ptt[0].pgn;
429         p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
430         i_cell = p_pgc->program_map[ pgn - 1 ] - 1;
431
432         p_area->i_start =
433             LB2OFF( p_dvd->p_cur_pgc->cell_playback[ i_cell ].first_sector );
434
435         intf_WarnMsg( 3, "dvdread: start %d vts_title %d pgc %d pgn %d",
436                          p_area->i_id, p_dvd->i_ttn, pgc_id, pgn );
437
438         /*
439          * Find title end
440          */
441         i_cell = p_dvd->p_cur_pgc->nr_of_cells - 1;
442
443         p_dvd->i_end_block = p_pgc->cell_playback[ i_cell ].last_sector;
444         p_area->i_size = LB2OFF( p_dvd->i_end_block )- p_area->i_start;
445
446         intf_WarnMsg( 12, "dvdread: start %lld size %lld end %d",
447                           p_area->i_start , p_area->i_size, p_dvd->i_end_block );
448
449         /*
450          * Set properties for current chapter
451          */
452         /* Remeber current chapter */
453         p_dvd->i_chapter = p_area->i_part;
454         p_dvd->b_eoc = 0;
455
456         pgc_id = p_vts->vts_ptt_srpt->title[
457                     p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
458         pgn = p_vts->vts_ptt_srpt->title[
459                     p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
460
461         p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
462         p_dvd->i_pack_len = 0;
463         p_dvd->i_next_cell = p_dvd->i_cur_cell = p_pgc->program_map[pgn-1] - 1;
464         DvdReadFindCell( p_dvd );
465
466         p_dvd->i_next_vobu = p_dvd->i_cur_block =
467             p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
468
469         /*
470          * Angle management
471          */
472         p_area->i_angle_nb = p_vmg->tt_srpt->title[p_area->i_id-1].nr_of_angles;
473         p_area->i_angle = main_GetIntVariable( INPUT_ANGLE_VAR, 1 );
474
475         if( ( p_area->i_angle <= 0 ) || p_area->i_angle > p_area->i_angle_nb )
476         {
477             p_area->i_angle = 1;
478         }
479         p_dvd->i_angle = p_area->i_angle;
480         p_dvd->i_angle_nb = p_area->i_angle_nb;
481
482         /*
483          * We've got enough info, time to open the title set data.
484          */
485         if( ! ( p_dvd->p_title = DVDOpenFile( p_dvd->p_dvdread,
486             p_vmg->tt_srpt->title[ p_area->i_id - 1 ].title_set_nr,
487             DVD_READ_TITLE_VOBS ) ) )
488         {
489             intf_ErrMsg( "dvdread error: can't open title (VTS_%02d_1.VOB)",
490                          p_vmg->tt_srpt->title[p_area->i_id-1].title_set_nr );
491             ifoClose( p_vts );
492             ifoClose( p_vmg );
493             DVDClose( p_dvd->p_dvdread );
494             return -1;
495         }
496
497 //        IfoPrintTitle( p_dvd );
498
499         /*
500          * Destroy obsolete ES by reinitializing program 0
501          * and find all ES in title with ifo data
502          */
503         if( p_input->stream.pp_programs != NULL )
504         {
505             /* We don't use input_EndStream here since
506              * we keep area structures */
507
508             for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
509             {
510                 input_UnselectES( p_input, p_input->stream.pp_selected_es[i] );
511             }
512
513             free( p_input->stream.pp_selected_es );
514             input_DelProgram( p_input, p_input->stream.pp_programs[0] );
515
516             p_input->stream.pp_selected_es = NULL;
517             p_input->stream.i_selected_es_number = 0;
518         }
519
520         input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
521
522         /* No PSM to read in DVD mode, we already have all information */
523         p_input->stream.pp_programs[0]->b_is_ok = 1;
524
525         p_es = NULL;
526
527         /* ES 0 -> video MPEG2 */
528 //        IfoPrintVideo( p_dvd );
529
530         p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
531         p_es->i_stream_id = 0xe0;
532         p_es->i_type = MPEG2_VIDEO_ES;
533         p_es->i_cat = VIDEO_ES;
534         if( p_main->b_video )
535         {
536             input_SelectES( p_input, p_es );
537         }
538
539 #define audio_control \
540     p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
541         /* Audio ES, in the order they appear in .ifo */
542         for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams ; i++ )
543         {
544             int i_position = 0;
545             u16 i_id;
546
547 //            IfoPrintAudio( p_dvd, i );
548
549             /* audio channel is active if first byte is 0x80 */
550             if( audio_control & 0x8000 )
551             {
552                 i_audio_nb++;
553                 i_position = ( audio_control & 0x7F00 ) >> 8;
554
555             intf_WarnMsg( 12, "dvd audio position  %d", i_position );
556                 switch( p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format )
557                 {
558                 case 0x00:              /* AC3 */
559                     i_id = ( ( 0x80 + i_position ) << 8 ) | 0xbd;
560                     p_es = input_AddES( p_input,
561                                p_input->stream.pp_programs[0], i_id, 0 );
562                     p_es->i_stream_id = 0xbd;
563                     p_es->i_type = AC3_AUDIO_ES;
564                     p_es->b_audio = 1;
565                     p_es->i_cat = AUDIO_ES;
566                     strcpy( p_es->psz_desc, DecodeLanguage( hton16(
567                         p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) ); 
568                     strcat( p_es->psz_desc, " (ac3)" );
569
570                     break;
571                 case 0x02:
572                 case 0x03:              /* MPEG audio */
573                     i_id = 0xc0 + i_position;
574                     p_es = input_AddES( p_input,
575                                     p_input->stream.pp_programs[0], i_id, 0 );
576                     p_es->i_stream_id = i_id;
577                     p_es->i_type = MPEG2_AUDIO_ES;
578                     p_es->b_audio = 1;
579                     p_es->i_cat = AUDIO_ES;
580                     strcpy( p_es->psz_desc, DecodeLanguage( hton16(
581                         p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) ); 
582                     strcat( p_es->psz_desc, " (mpeg)" );
583
584                     break;
585                 case 0x04:              /* LPCM */
586
587                     i_id = ( ( 0xa0 + i_position ) << 8 ) | 0xbd;
588                     p_es = input_AddES( p_input,
589                                     p_input->stream.pp_programs[0], i_id, 0 );
590                     p_es->i_stream_id = i_id;
591                     p_es->i_type = LPCM_AUDIO_ES;
592                     p_es->b_audio = 1;
593                     p_es->i_cat = AUDIO_ES;
594                     strcpy( p_es->psz_desc, DecodeLanguage( hton16(
595                         p_vts->vtsi_mat->vts_audio_attr[i-1].lang_code ) ) ); 
596                     strcat( p_es->psz_desc, " (lpcm)" );
597
598                     break;
599                 case 0x06:              /* DTS */
600                     i_id = ( ( 0x88 + i_position ) << 8 ) | 0xbd;
601                     intf_ErrMsg( "dvd warning: DTS audio not handled yet"
602                                  "(0x%x)", i_id );
603                     break;
604                 default:
605                     i_id = 0;
606                     intf_ErrMsg( "dvd warning: unknown audio type %.2x",
607                              p_vts->vtsi_mat->vts_audio_attr[i-1].audio_format );
608                 }
609             }
610         }
611 #undef audio_control
612 #define spu_control \
613     p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
614
615         /* Sub Picture ES */
616
617         for( i = 1 ; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
618         {
619             int i_position = 0;
620             u16 i_id;
621
622 //            IfoPrintSpu( p_dvd, i );
623             intf_WarnMsg( 12, "dvd spu %d 0x%02x", i, spu_control );
624
625             if( spu_control & 0x80000000 )
626             {
627                 i_spu_nb++;
628
629                 /*  there are several streams for one spu */
630                 if(  p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
631                 {
632                     /* 16:9 */
633                     switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
634                     {
635                     case 1:
636                         i_position = spu_control & 0xff;
637                         break;
638                     case 2:
639                         i_position = ( spu_control >> 8 ) & 0xff;
640                         break;
641                     default:
642                         i_position = ( spu_control >> 16 ) & 0xff;
643                         break;
644                     }
645                 }
646                 else
647                 {
648                     /* 4:3 */
649                     i_position = ( spu_control >> 24 ) & 0x7F;
650                 }
651
652                 i_id = ( ( 0x20 + i_position ) << 8 ) | 0xbd;
653                 p_es = input_AddES( p_input,
654                                     p_input->stream.pp_programs[0], i_id, 0 );
655                 p_es->i_stream_id = 0xbd;
656                 p_es->i_type = DVD_SPU_ES;
657                 p_es->i_cat = SPU_ES;
658                 strcpy( p_es->psz_desc, DecodeLanguage( hton16(
659                     p_vts->vtsi_mat->vts_subp_attr[i-1].lang_code ) ) ); 
660             }
661         }
662 #undef spu_control
663         if( p_main->b_audio )
664         {
665             /* For audio: first one if none or a not existing one specified */
666             int i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
667             if( i_audio < 0 || i_audio > i_audio_nb )
668             {
669                 main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
670                 i_audio = 1;
671             }
672             if( i_audio > 0 && i_audio_nb > 0 )
673             {
674                 if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) ||
675                     ( main_GetIntVariable( INPUT_AUDIO_VAR, 0 ) ==
676                       REQUESTED_AC3 ) )
677                 {
678                     int     i_ac3 = i_audio;
679                     while( ( p_input->stream.pp_es[i_ac3]->i_type !=
680                              AC3_AUDIO_ES ) && ( i_ac3 <=
681                              p_vts->vtsi_mat->nr_of_vts_audio_streams ) )
682                     {
683                         i_ac3++;
684                     }
685                     if( p_input->stream.pp_es[i_ac3]->i_type == AC3_AUDIO_ES )
686                     {
687                         input_SelectES( p_input,
688                                         p_input->stream.pp_es[i_ac3] );
689                     }
690                 }
691                 else
692                 {
693                     input_SelectES( p_input,
694                                     p_input->stream.pp_es[i_audio] );
695                 }
696             }
697         }
698
699         if( p_main->b_video )
700         {
701             /* for spu, default is none */
702             int i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );
703             if( i_spu < 0 || i_spu > i_spu_nb )
704             {
705                 main_PutIntVariable( INPUT_SUBTITLE_VAR, 0 );
706                 i_spu = 0;
707             }
708             if( i_spu > 0 && i_spu_nb > 0 )
709             {
710                 i_spu += p_vts->vtsi_mat->nr_of_vts_audio_streams;
711                 input_SelectES( p_input, p_input->stream.pp_es[i_spu] );
712             }
713         }
714     } /* i_title >= 0 */
715     else
716     {
717         p_area = p_input->stream.p_selected_area;
718     }
719
720     /*
721      * Chapter selection
722      */
723
724     if( p_area->i_part != p_dvd->i_chapter )
725     {
726         if( ( p_area->i_part > 0 ) &&
727             ( p_area->i_part <= p_area->i_part_nb ))
728         {
729             p_dvd->i_ttn = p_vmg->tt_srpt->title[p_area->i_id-1].vts_ttn;
730             pgc_id = p_vts->vts_ptt_srpt->title[
731                         p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgcn;
732             pgn = p_vts->vts_ptt_srpt->title[
733                         p_dvd->i_ttn-1].ptt[p_area->i_part-1].pgn;
734
735             p_pgc = p_vts->vts_pgcit->pgci_srp[ pgc_id - 1 ].pgc;
736
737             p_dvd->i_cur_cell = p_pgc->program_map[ pgn - 1 ] - 1;
738             p_dvd->i_chapter = p_area->i_part;
739             DvdReadFindCell( p_dvd );
740
741             p_dvd->i_pack_len = 0;
742             p_dvd->i_next_vobu = p_dvd->i_cur_block =
743                     p_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
744         }
745         else
746         {
747             p_area->i_part = p_dvd->i_chapter;
748         }
749     }
750 #undef p_vts
751 #undef p_vmg
752
753     if( p_area->i_angle != p_dvd->i_angle )
754     {
755         p_dvd->i_angle = p_area->i_angle;
756
757         intf_WarnMsg( 3, "dvd info: angle %d selected", p_area->i_angle );
758     }
759     /* warn interface that something has changed */
760     p_area->i_tell = LB2OFF( p_dvd->i_next_vobu ) - p_area->i_start;
761     p_input->stream.b_seekable = 1;
762     p_input->stream.b_changed = 1;
763
764     return 0;
765 }
766
767
768 /*****************************************************************************
769  * DvdReadRead: reads data packets into the netlist.
770  *****************************************************************************
771  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
772  * EOF.
773  *****************************************************************************/
774 static int DvdReadRead( input_thread_t * p_input,
775                         data_packet_t ** pp_packets )
776 {
777     thread_dvd_data_t *     p_dvd;
778     netlist_t *             p_netlist;
779     u8                      p_data[DVD_VIDEO_LB_LEN];
780     struct iovec *          p_vec;
781     struct data_packet_s *  pp_data[DVD_DATA_READ_ONCE];
782     u8 *                    pi_cur;
783     int                     i_blocks;
784     int                     i_read;
785     int                     i_iovec;
786     int                     i_packet_size;
787     int                     i_packet;
788     int                     i_pos;
789
790     p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
791     p_netlist = (netlist_t *)p_input->p_method_data;
792
793     /*
794      * Playback by cell in this pgc, starting at the cell for our chapter.
795      */
796
797     /* 
798      * End of pack, we select the following one
799      */
800     if( ! p_dvd->i_pack_len )
801     {
802         /*
803          * Read NAV packet.
804          */
805         if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
806                        1, p_data ) ) != 1 )
807         {
808             intf_ErrMsg( "dvdread error: read failed for block %d",
809                          p_dvd->i_next_vobu );
810             return -1;
811         }
812
813         assert( p_data[41] == 0xbf && p_data[1027] == 0xbf );
814
815         /*
816          * Parse the contained dsi packet.
817          */
818
819         DvdReadHandleDSI( p_dvd, p_data );
820
821         /* End of File */
822         if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
823         {
824             return 1;
825         }
826
827         assert( p_dvd->i_pack_len < 1024 );
828         /* Ugly kludge: we send the pack block to the input for it
829          * sometimes has a zero scr and restart the sync */
830 //        p_dvd->i_cur_block ++;
831         p_dvd->i_pack_len++;
832
833     }
834
835     /*
836      * Compute the number of blocks to read
837      */
838     i_blocks = p_dvd->i_pack_len >= DVD_BLOCK_READ_ONCE
839              ? DVD_BLOCK_READ_ONCE : p_dvd->i_pack_len;
840     p_dvd->i_pack_len -= i_blocks;
841     p_netlist->i_read_once = i_blocks;
842
843     /* Get an iovec pointer */
844     if( ( p_vec = input_NetlistGetiovec( p_netlist ) ) == NULL )
845     {
846         intf_ErrMsg( "dvdread error: can't get iovec" );
847         return -1;
848     }
849
850     /* Reads from DVD */
851     i_read = DVDReadVBlocks( p_dvd->p_title, p_dvd->i_cur_block,
852                              i_blocks, p_vec );
853     if( i_read != i_blocks )
854     {
855         intf_ErrMsg( "dvdread error: read failed for %d/%d blocks at 0x%02x",
856                      i_read, i_blocks, p_dvd->i_cur_block );
857         return -1;
858     }
859
860     p_dvd->i_cur_block += i_read;
861 /*
862     intf_WarnMsg( 12, "dvdread i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
863 */
864     /* Update netlist indexes: we don't do it in DVDGetiovec since we
865      * need know the real number of blocks read */
866     input_NetlistMviovec( p_netlist, i_read, pp_data );
867
868     i_packet = 0;
869
870     /* Read headers to compute payload length */
871     for( i_iovec = 0 ; i_iovec < i_read ; i_iovec++ )
872     {
873         i_pos = 0;
874
875         while( i_pos < p_netlist->i_buffer_size )
876         {
877             pi_cur = (u8*)p_vec[i_iovec].iov_base + i_pos;
878
879             /*default header */
880             if( U32_AT( pi_cur ) != 0x1BA )
881             {
882                 /* That's the case for all packets, except pack header. */
883                 i_packet_size = U16_AT( pi_cur + 4 );
884                 pp_packets[i_packet] = input_NetlistNewPtr( p_netlist );
885                 (*pp_data[i_iovec]->pi_refcount)++;
886                 pp_packets[i_packet]->pi_refcount =
887                     pp_data[i_iovec]->pi_refcount;
888                 pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;
889             }
890             else
891             {
892                 /* MPEG-2 Pack header. */
893                 i_packet_size = 8;
894                 pp_packets[i_packet] = pp_data[i_iovec];
895
896             }
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( p_dvd->i_cur_block ) -
918             p_input->stream.p_selected_area->i_start;
919
920     if( p_dvd->b_eoc )
921     {
922         /* We modify i_part only at end of chapter not to erase
923          * some modification from the interface */
924         p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
925         p_dvd->b_eoc = 0;
926     }
927
928
929     vlc_mutex_unlock( &p_input->stream.stream_lock );
930
931     return 0;
932 }
933 #undef p_pgc
934
935 /*****************************************************************************
936  * DVDRewind : reads a stream backward
937  *****************************************************************************/
938 static int DvdReadRewind( input_thread_t * p_input )
939 {
940     return( -1 );
941 }
942
943 /*****************************************************************************
944  * DvdReadSeek : Goes to a given position on the stream.
945  *****************************************************************************
946  * This one is used by the input and translate chronological position from
947  * input to logical position on the device.
948  * The lock should be taken before calling this function.
949  *****************************************************************************/
950 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
951 {
952     thread_dvd_data_t *     p_dvd;
953     int                     i_lb;
954     int                     i_tmp;
955     int                     i_chapter = 0;
956     int                     i_cell = 0;
957     int                     i_vobu = 0;
958     int                     i_sub_cell = 0;
959
960     i_off += p_input->stream.p_selected_area->i_start;
961     i_lb = OFF2LB( i_off );
962     p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
963
964     /* find cell */
965     while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
966     {
967         i_cell++;
968     }
969
970     /* find chapter */
971     do
972     {
973         pgc_t *     p_pgc;
974         int         pgc_id, pgn;
975
976         i_chapter++;
977         pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
978                     p_dvd->i_ttn-1].ptt[i_chapter].pgcn;
979         pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
980                     p_dvd->i_ttn-1].ptt[i_chapter].pgn;
981
982         p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
983         i_tmp = p_pgc->program_map[pgn-1];
984
985     } while( i_tmp <= i_cell );
986
987     /* find vobu */
988     while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
989             <= i_lb )
990     {
991         i_vobu++;
992     }
993
994     /* find sub_cell */
995     while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
996             p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
997     {
998         i_sub_cell++;
999     }
1000
1001 /*
1002     intf_WarnMsg(12, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1003             i_cell, i_sub_cell,i_chapter, i_vobu,
1004             p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1005             p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1006             p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1007 */
1008     p_dvd->i_cur_block = i_lb;
1009     p_dvd->i_next_vobu =
1010         p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1011     p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1012     p_dvd->i_cur_cell = i_cell;
1013     p_dvd->i_chapter = i_chapter;
1014     DvdReadFindCell( p_dvd );
1015
1016     p_input->stream.p_selected_area->i_tell =
1017         LB2OFF ( p_dvd->i_cur_block )
1018          - p_input->stream.p_selected_area->i_start;
1019     p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1020
1021     return;
1022 }
1023
1024 /*****************************************************************************
1025  * DvdReadHandleDSI
1026  *****************************************************************************/
1027 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data )
1028 {
1029     navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1030
1031     /*
1032      * Determine where we go next.  These values are the ones we mostly
1033      * care about.
1034      */
1035     p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1036
1037     /*
1038      * If we're not at the end of this cell, we can determine the next
1039      * VOBU to display using the VOBU_SRI information section of the
1040      * DSI.  Using this value correctly follows the current angle,
1041      * avoiding the doubled scenes in The Matrix, and makes our life
1042      * really happy.
1043      */
1044     if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1045     {
1046 #if 1
1047         switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1048         {
1049             case 0x4:
1050                 /* interleaved unit with no angle */
1051                 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != -1 )
1052                 {
1053                     p_dvd->i_next_vobu = p_dvd->i_cur_block +
1054                         p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1055                     p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1056                 }
1057                 else
1058                 {
1059                     p_dvd->i_next_vobu = p_dvd->i_cur_block +
1060                         p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1061                     p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1062                 }
1063                 break;
1064             case 0x5:
1065                 /* vobu is end of ilvu */
1066                 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1067                 {
1068                     p_dvd->i_next_vobu = p_dvd->i_cur_block +
1069                         p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1070                     p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1071
1072                     break;
1073                 }
1074             case 0x6:
1075                 /* vobu is beginning of ilvu */
1076             case 0x9:
1077                 /* next scr is 0 */
1078             case 0xa:
1079                 /* entering interleaved section */
1080             case 0x8:
1081                 /* non interleaved cells in interleaved section */
1082             default:
1083                 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1084                     ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1085                 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1086                 break;
1087         }
1088 #else
1089         p_dvd->i_next_vobu = p_dvd->i_cur_block +
1090             ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1091         p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1092 #endif
1093     }
1094     else
1095     {
1096         p_dvd->i_cur_cell = p_dvd->i_next_cell;
1097         DvdReadFindCell( p_dvd );
1098
1099         p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1100         p_dvd->i_next_vobu =
1101             p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1102     }
1103
1104 #if 0
1105     intf_WarnMsg( 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1106             p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1107             p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1108             p_dvd->dsi_pack.dsi_gi.vobu_ea,
1109             p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1110             p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1111
1112     intf_WarnMsg( 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d", 
1113             p_dvd->dsi_pack.sml_pbi.category,
1114             p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1115             p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1116             p_dvd->dsi_pack.sml_pbi.size );
1117
1118     intf_WarnMsg( 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1119             p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1120             p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1121             p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1122 #endif
1123 }
1124
1125 /*****************************************************************************
1126  * DvdReadFindCell
1127  *****************************************************************************/
1128 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1129 {
1130     int         pgc_id, pgn;
1131     int         i = 0;
1132     pgc_t *     p_pgc;
1133 #define cell p_dvd->p_cur_pgc->cell_playback
1134     if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1135     {
1136 #if 0
1137         p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1138         p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1139 #else
1140         p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1141
1142         while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1143         {
1144             i++;
1145         }
1146         p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1147 #endif
1148     }
1149     else
1150     {
1151         p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1152     }
1153 #undef cell
1154     pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1155                 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter].pgcn;
1156     pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1157                 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter].pgn;
1158     p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1159
1160     if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )
1161     {
1162         p_dvd->i_chapter++;
1163         p_dvd->b_eoc = 1;
1164     }
1165 }