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