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