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