]> git.sesse.net Git - vlc/blob - plugins/dvdread/input_dvdread.c
* ALL: got rid of *_Probe functions because most of them were duplicates
[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.20 2002/02/15 13:32:53 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
77 /*****************************************************************************
78  * Local prototypes
79  *****************************************************************************/
80 /* called from outside */
81 static int  DvdReadProbe    ( struct input_thread_s * );
82 static void DvdReadInit     ( struct input_thread_s * );
83 static void DvdReadEnd      ( struct input_thread_s * );
84 static void DvdReadOpen     ( struct input_thread_s * );
85 static void DvdReadClose    ( struct input_thread_s * );
86 static int  DvdReadSetArea  ( struct input_thread_s *, struct input_area_s * );
87 static int  DvdReadSetProgram( struct input_thread_s *, pgrm_descriptor_t * );
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     input.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_set_program      = DvdReadSetProgram;
127     input.pf_demux            = input_DemuxPS;
128     input.pf_new_packet       = input_NewPacket;
129     input.pf_new_pes          = input_NewPES;
130     input.pf_delete_packet    = input_DeletePacket;
131     input.pf_delete_pes       = input_DeletePES;
132     input.pf_rewind           = DvdReadRewind;
133     input.pf_seek             = DvdReadSeek;
134
135 #undef input
136 }
137
138 /*
139  * Data reading functions
140  */
141
142 /*****************************************************************************
143  * DvdReadProbe: verifies that the stream is a PS stream
144  *****************************************************************************/
145 static int DvdReadProbe( input_thread_t *p_input )
146 {
147     char * psz_name = p_input->p_source;
148
149     if((( strlen(psz_name) > 8 ) && !strncasecmp( psz_name, "dvdread:", 8 ))
150      ||(( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 )))
151     {
152         return 0;
153     }
154
155     return -1;
156 }
157
158 /*****************************************************************************
159  * DvdReadInit: initializes DVD structures
160  *****************************************************************************/
161 static void DvdReadInit( input_thread_t * p_input )
162 {
163     thread_dvd_data_t *  p_dvd;
164     input_area_t *       p_area;
165     int                  i_title;
166     int                  i_chapter;
167     int                  i;
168
169     p_dvd = malloc( sizeof(thread_dvd_data_t) );
170     if( p_dvd == NULL )
171     {
172         intf_ErrMsg( "dvdread error: out of memory" );
173         p_input->b_error = 1;
174         return;
175     }
176
177     /* we take the pointer to dvd_reader_t back  */
178     p_dvd->p_dvdread = (dvd_reader_t *)p_input->p_plugin_data;
179     p_dvd->p_title = NULL;
180     p_dvd->p_vts_file = NULL;
181
182     p_input->p_plugin_data = (void *)p_dvd;
183
184     if( (p_input->p_method_data = input_BuffersInit()) == NULL )
185     {
186         free( p_dvd );
187         p_input->b_error = 1;
188         return;
189     }
190
191     /* We read DVD_BLOCK_READ_ONCE in each loop */
192     p_dvd->i_block_once = DVD_BLOCK_READ_ONCE;
193
194     /* Ifo allocation & initialisation */
195     if( ! ( p_dvd->p_vmg_file = ifoOpen( p_dvd->p_dvdread, 0 ) ) )
196     {
197         intf_ErrMsg( "dvdread error: can't open VMG info" );
198         input_BuffersEnd( p_input->p_method_data );
199         free( p_dvd );
200         p_input->b_error = 1;
201         return;
202     }
203     intf_WarnMsg( 2, "dvdread info: VMG opened" );
204
205     /* Set stream and area data */
206     vlc_mutex_lock( &p_input->stream.stream_lock );
207
208     /* Initialize ES structures */
209     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
210
211     /* disc input method */
212     p_input->stream.i_method = INPUT_METHOD_DVD;
213
214 #define tt_srpt p_dvd->p_vmg_file->tt_srpt
215     intf_WarnMsg( 2, "dvdread info: number of titles: %d", tt_srpt->nr_of_srpts );
216
217 #define area p_input->stream.pp_areas
218     /* We start from 1 here since the default area 0
219      * is reserved for video_ts.vob */
220     for( i = 1 ; i <= tt_srpt->nr_of_srpts ; i++ )
221     {
222         input_AddArea( p_input );
223
224         /* Titles are Program Chains */
225         area[i]->i_id = i;
226
227         /* Absolute start offset and size
228          * We can only set that with vts ifo, so we do it during the
229          * first call to DVDSetArea */
230         area[i]->i_start = 0;
231         area[i]->i_size = 0;
232
233         /* Number of chapters */
234         area[i]->i_part_nb = tt_srpt->title[i-1].nr_of_ptts;
235         area[i]->i_part = 1;
236
237         /* Number of angles */
238         area[i]->i_angle_nb = 0;
239         area[i]->i_angle = 1;
240
241         area[i]->i_plugin_data = tt_srpt->title[i-1].title_set_nr;
242     }
243 #undef area
244
245     /* Get requested title - if none try the first title */
246     i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
247     if( i_title <= 0 || i_title > tt_srpt->nr_of_srpts )
248     {
249         i_title = 1;
250     }
251
252 #undef tt_srpt
253
254     /* Get requested chapter - if none defaults to first one */
255     i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
256     if( i_chapter <= 0 )
257     {
258         i_chapter = 1;
259     }
260
261     p_input->stream.pp_areas[i_title]->i_part = i_chapter;
262
263     p_area = p_input->stream.pp_areas[i_title];
264
265     /* set title, chapter, audio and subpic */
266     DvdReadSetArea( p_input, p_area );
267
268     vlc_mutex_unlock( &p_input->stream.stream_lock );
269
270     return;
271 }
272
273 /*****************************************************************************
274  * DvdReadOpen: open libdvdread
275  *****************************************************************************/
276 static void DvdReadOpen( struct input_thread_s *p_input )
277 {
278     dvd_reader_t    *p_dvdread;
279
280     vlc_mutex_lock( &p_input->stream.stream_lock );
281
282     p_input->stream.i_method = INPUT_METHOD_DVD;
283
284     /* If we are here we can control the pace... */
285     p_input->stream.b_pace_control = 1;
286     p_input->stream.b_seekable = 1;
287     
288     p_input->stream.p_selected_area->i_size = 0;
289     p_input->stream.p_selected_area->i_tell = 0;
290
291     vlc_mutex_unlock( &p_input->stream.stream_lock );
292
293     /* XXX: put this shit in an access plugin */
294     if( strlen( p_input->p_source ) > 8
295          && !strncasecmp( p_input->p_source, "dvdread:", 8 ) )
296     {
297         p_dvdread = DVDOpen( p_input->p_source + 8 );
298     }
299     else if( strlen( p_input->p_source ) > 4
300          && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
301     {
302         p_dvdread = DVDOpen( p_input->p_source + 4 );
303     }
304     else
305     {
306         p_dvdread = DVDOpen( p_input->p_source );
307     }
308
309     if( ! p_dvdread )
310     {
311         p_input->b_error = 1;
312         return;
313     }
314
315     /* the vlc input doesn't handle the file descriptor with libdvdread */
316     p_input->i_handle = -1;
317
318     /* p_dvdread is stored in p_plugin_data until we have
319      * malloc'ed a dvd_thread_data_t */
320     p_input->p_plugin_data = (void *) p_dvdread;
321 }
322
323 /*****************************************************************************
324  * DvdReadClose: close libdvdread
325  *****************************************************************************/
326 static void DvdReadClose( struct input_thread_s *p_input )
327 {
328     DVDClose( ((thread_dvd_data_t*)p_input->p_plugin_data)->p_dvdread );
329     free( p_input->p_plugin_data );
330     p_input->p_plugin_data = NULL;
331
332 }
333
334 /*****************************************************************************
335  * DvdReadEnd: frees unused data
336  *****************************************************************************/
337 static void DvdReadEnd( input_thread_t * p_input )
338 {
339     thread_dvd_data_t *     p_dvd;
340
341     p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
342
343     /* close libdvdread */
344     DVDCloseFile( p_dvd->p_title );
345     ifoClose( p_dvd->p_vts_file );
346     ifoClose( p_dvd->p_vmg_file );
347
348     input_BuffersEnd( p_input->p_method_data );
349 }
350
351 /*****************************************************************************
352  * DvdReadSetProgram: Does nothing, a DVD is mono-program
353  *****************************************************************************/
354 static int DvdReadSetProgram( input_thread_t * p_input,
355                               pgrm_descriptor_t * p_program )
356 {
357     return 0;
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.p_selected_program );
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         p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
525
526         /* No PSM to read in DVD mode, we already have all information */
527         p_input->stream.p_selected_program->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.p_selected_program, 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.p_selected_program, 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.p_selected_program, 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.p_selected_program, 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.p_selected_program, 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_data )
780 {
781     thread_dvd_data_t *     p_dvd;
782     u8                      p_data[DVD_VIDEO_LB_LEN];
783     struct iovec            p_vec[DVD_BLOCK_READ_ONCE];
784     u8 *                    pi_cur;
785     int                     i_blocks;
786     int                     i_read;
787     int                     i_iovec;
788     int                     i_packet_size;
789     int                     i_packet;
790     int                     i_pos;
791     data_packet_t *         p_data_p;
792     boolean_t               b_eot = 0;
793
794     p_dvd = (thread_dvd_data_t *)p_input->p_plugin_data;
795
796     *pp_data = NULL;
797
798     /*
799      * Playback by cell in this pgc, starting at the cell for our chapter.
800      */
801
802     /* 
803      * End of pack, we select the following one
804      */
805     if( ! p_dvd->i_pack_len )
806     {
807         /*
808          * Read NAV packet.
809          */
810         if( ( i_read = DVDReadBlocks( p_dvd->p_title, p_dvd->i_next_vobu,
811                        1, p_data ) ) != 1 )
812         {
813             intf_ErrMsg( "dvdread error: read failed for block %d",
814                          p_dvd->i_next_vobu );
815             return -1;
816         }
817
818         /* basic check to be sure we don't have a empty title
819          * go to next title if so */
820         assert( p_data[41] == 0xbf && p_data[1027] == 0xbf );
821         if( p_data[41] == 0xbf && p_data[1027] == 0xbf )
822         {
823             /*
824              * Parse the contained dsi packet.
825              */
826
827             DvdReadHandleDSI( p_dvd, p_data );
828
829             /* End of File */
830             if( p_dvd->i_next_vobu >= p_dvd->i_end_block + 1 )
831             {
832                 return 1;
833             }
834
835             assert( p_dvd->i_pack_len < 1024 );
836             /* FIXME: Ugly kludge: we send the pack block to the input for it
837              * sometimes has a zero scr and restart the sync */
838             //p_dvd->i_cur_block ++;
839             p_dvd->i_pack_len++;
840         }
841         else
842         {
843             b_eot = 1;
844             p_dvd->i_pack_len = 0;
845             return 1;
846         }
847     }
848
849     /*
850      * Compute the number of blocks to read
851      */
852     i_blocks = p_dvd->i_pack_len >= DVD_BLOCK_READ_ONCE
853              ? DVD_BLOCK_READ_ONCE : p_dvd->i_pack_len;
854     p_dvd->i_pack_len -= i_blocks;
855
856     /* Get iovecs */
857     *pp_data = p_data_p = input_BuffersToIO( p_input->p_method_data, p_vec,
858                                              DVD_BLOCK_READ_ONCE );
859
860     if ( p_data_p == NULL )
861     {
862         return( -1 );
863     }
864
865     /* Reads from DVD */
866     i_read = DVDReadVBlocks( p_dvd->p_title, p_dvd->i_cur_block,
867                              i_blocks, p_vec );
868     if( i_read != i_blocks )
869     {
870         intf_ErrMsg( "dvdread error: read failed for %d/%d blocks at 0x%02x",
871                      i_read, i_blocks, p_dvd->i_cur_block );
872         return -1;
873     }
874
875     p_dvd->i_cur_block += i_read;
876 /*
877     intf_WarnMsg( 12, "dvdread i_blocks: %d len: %d current: 0x%02x", i_read, p_dvd->i_pack_len, p_dvd->i_cur_block );
878 */
879     i_packet = 0;
880
881     /* Read headers to compute payload length */
882     for( i_iovec = 0 ; i_iovec < i_read ; i_iovec++ )
883     {
884         data_packet_t * p_current = p_data_p;
885         i_pos = 0;
886
887         while( i_pos < DVD_VIDEO_LB_LEN )
888         {
889             pi_cur = (u8*)p_vec[i_iovec].iov_base + i_pos;
890
891             /*default header */
892             if( U32_AT( pi_cur ) != 0x1BA )
893             {
894                 /* That's the case for all packets, except pack header. */
895                 i_packet_size = U16_AT( pi_cur + 4 );
896             }
897             else
898             {
899                 /* MPEG-2 Pack header. */
900                 i_packet_size = 8;
901             }
902             if( i_pos != 0 )
903             {
904                 *pp_data = input_ShareBuffer( p_input->p_method_data,
905                                               p_current );
906             }
907             else
908             {
909                 *pp_data = p_data_p;
910                 p_data_p = p_data_p->p_next;
911             }
912
913             (*pp_data)->p_payload_start = (*pp_data)->p_demux_start =
914                     (*pp_data)->p_demux_start + i_pos;
915             
916
917             (*pp_data)->p_payload_end =
918                     (*pp_data)->p_payload_start + i_packet_size + 6;
919
920 //            pp_packets[i_packet]->p_next = NULL;
921 //            pp_packets[i_packet]->b_discard_payload = 0;
922
923             i_packet++;
924             i_pos += i_packet_size + 6;
925             pp_data = &(*pp_data)->p_next;
926         }
927     }
928
929     p_input->pf_delete_packet( p_input->p_method_data, p_data_p );
930     *pp_data = NULL;
931
932     vlc_mutex_lock( &p_input->stream.stream_lock );
933
934     p_input->stream.p_selected_area->i_tell =
935         LB2OFF( p_dvd->i_cur_block ) -
936             p_input->stream.p_selected_area->i_start;
937
938     if( p_dvd->b_eoc )
939     {
940         /* We modify i_part only at end of chapter not to erase
941          * some modification from the interface */
942         p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
943         p_dvd->b_eoc = 0;
944     }
945     
946     if( p_input->stream.p_selected_area->i_tell
947             >= p_input->stream.p_selected_area->i_size || b_eot )
948     {
949         if( ( p_input->stream.p_selected_area->i_id + 1 ) >= 
950                         p_input->stream.i_area_nb )
951         {
952             /* EOF */
953             vlc_mutex_unlock( &p_input->stream.stream_lock );
954             return 1;
955         }
956
957         /* EOT */
958         intf_WarnMsg( 4, "dvd info: new title" );
959         DvdReadSetArea( p_input, p_input->stream.pp_areas[
960                         p_input->stream.p_selected_area->i_id+1] );
961         vlc_mutex_unlock( &p_input->stream.stream_lock );
962         return 0;
963     }
964
965     vlc_mutex_unlock( &p_input->stream.stream_lock );
966
967     return i_read;
968 }
969 #undef p_pgc
970
971 /*****************************************************************************
972  * DVDRewind : reads a stream backward
973  *****************************************************************************/
974 static int DvdReadRewind( input_thread_t * p_input )
975 {
976     return( -1 );
977 }
978
979 /*****************************************************************************
980  * DvdReadSeek : Goes to a given position on the stream.
981  *****************************************************************************
982  * This one is used by the input and translate chronological position from
983  * input to logical position on the device.
984  * The lock should be taken before calling this function.
985  *****************************************************************************/
986 static void DvdReadSeek( input_thread_t * p_input, off_t i_off )
987 {
988     thread_dvd_data_t *     p_dvd;
989     int                     i_lb;
990     int                     i_tmp;
991     int                     i_chapter = 0;
992     int                     i_cell = 0;
993     int                     i_vobu = 0;
994     int                     i_sub_cell = 0;
995
996     i_off += p_input->stream.p_selected_area->i_start;
997     i_lb = OFF2LB( i_off );
998     p_dvd = ( thread_dvd_data_t * )p_input->p_plugin_data;
999
1000     /* find cell */
1001     while( p_dvd->p_cur_pgc->cell_playback[i_cell].last_sector < i_lb )
1002     {
1003         i_cell++;
1004     }
1005
1006     /* find chapter */
1007     do
1008     {
1009         pgc_t *     p_pgc;
1010         int         pgc_id, pgn;
1011
1012         i_chapter++;
1013         pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1014                     p_dvd->i_ttn-1].ptt[i_chapter-1].pgcn;
1015         pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1016                     p_dvd->i_ttn-1].ptt[i_chapter-1].pgn;
1017
1018         p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1019         i_tmp = p_pgc->program_map[pgn-1];
1020
1021     } while( i_tmp <= i_cell );
1022
1023     /* find vobu */
1024     while( p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu]
1025             <= i_lb )
1026     {
1027         i_vobu++;
1028     }
1029
1030     /* find sub_cell */
1031     while( p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1032             p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1033     {
1034         i_sub_cell++;
1035     }
1036
1037 /*
1038     intf_WarnMsg(12, "cell %d i_sub_cell %d chapter %d vobu %d cell_sector %d vobu_sector %d sub_cell_sector %d",
1039             i_cell, i_sub_cell,i_chapter, i_vobu,
1040             p_dvd->p_cur_pgc->cell_playback[i_cell].first_sector,
1041             p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu],
1042             p_dvd->p_vts_file->vts_c_adt->cell_adr_table[i_sub_cell-1].start_sector);
1043 */
1044     p_dvd->i_cur_block = i_lb;
1045     p_dvd->i_next_vobu =
1046         p_dvd->p_vts_file->vts_vobu_admap->vobu_start_sectors[i_vobu];
1047     p_dvd->i_pack_len = p_dvd->i_next_vobu - i_lb;
1048     p_dvd->i_cur_cell = i_cell;
1049     p_dvd->i_chapter = i_chapter;
1050     DvdReadFindCell( p_dvd );
1051
1052     p_input->stream.p_selected_area->i_tell =
1053         LB2OFF ( p_dvd->i_cur_block )
1054          - p_input->stream.p_selected_area->i_start;
1055     p_input->stream.p_selected_area->i_part = p_dvd->i_chapter;
1056
1057     return;
1058 }
1059
1060 /*****************************************************************************
1061  * DvdReadHandleDSI
1062  *****************************************************************************/
1063 static void DvdReadHandleDSI( thread_dvd_data_t * p_dvd, u8 * p_data )
1064 {
1065     navRead_DSI( &(p_dvd->dsi_pack), &(p_data[ DSI_START_BYTE ]) );
1066
1067     /*
1068      * Determine where we go next.  These values are the ones we mostly
1069      * care about.
1070      */
1071     p_dvd->i_cur_block = p_dvd->dsi_pack.dsi_gi.nv_pck_lbn;
1072
1073     /*
1074      * If we're not at the end of this cell, we can determine the next
1075      * VOBU to display using the VOBU_SRI information section of the
1076      * DSI.  Using this value correctly follows the current angle,
1077      * avoiding the doubled scenes in The Matrix, and makes our life
1078      * really happy.
1079      */
1080     if( p_dvd->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1081     {
1082 #if 1
1083         switch( ( p_dvd->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1084         {
1085             case 0x4:
1086                 /* interleaved unit with no angle */
1087                 if( p_dvd->dsi_pack.sml_pbi.ilvu_sa != -1 )
1088                 {
1089                     p_dvd->i_next_vobu = p_dvd->i_cur_block +
1090                         p_dvd->dsi_pack.sml_pbi.ilvu_sa;
1091                     p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1092                 }
1093                 else
1094                 {
1095                     p_dvd->i_next_vobu = p_dvd->i_cur_block +
1096                         p_dvd->dsi_pack.dsi_gi.vobu_ea + 1;
1097                     p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1098                 }
1099                 break;
1100             case 0x5:
1101                 /* vobu is end of ilvu */
1102                 if( p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address )
1103                 {
1104                     p_dvd->i_next_vobu = p_dvd->i_cur_block +
1105                         p_dvd->dsi_pack.sml_agli.data[p_dvd->i_angle-1].address;
1106                     p_dvd->i_pack_len = p_dvd->dsi_pack.sml_pbi.ilvu_ea;
1107
1108                     break;
1109                 }
1110             case 0x6:
1111                 /* vobu is beginning of ilvu */
1112             case 0x9:
1113                 /* next scr is 0 */
1114             case 0xa:
1115                 /* entering interleaved section */
1116             case 0x8:
1117                 /* non interleaved cells in interleaved section */
1118             default:
1119                 p_dvd->i_next_vobu = p_dvd->i_cur_block +
1120                     ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1121                 p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1122                 break;
1123         }
1124 #else
1125         p_dvd->i_next_vobu = p_dvd->i_cur_block +
1126             ( p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1127         p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1128 #endif
1129     }
1130     else
1131     {
1132         p_dvd->i_cur_cell = p_dvd->i_next_cell;
1133         DvdReadFindCell( p_dvd );
1134
1135         p_dvd->i_pack_len = p_dvd->dsi_pack.dsi_gi.vobu_ea;
1136         p_dvd->i_next_vobu =
1137             p_dvd->p_cur_pgc->cell_playback[p_dvd->i_cur_cell].first_sector;
1138     }
1139
1140 #if 0
1141     intf_WarnMsg( 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1142             p_dvd->dsi_pack.dsi_gi.nv_pck_scr,
1143             p_dvd->dsi_pack.dsi_gi.nv_pck_lbn,
1144             p_dvd->dsi_pack.dsi_gi.vobu_ea,
1145             p_dvd->dsi_pack.dsi_gi.vobu_vob_idn,
1146             p_dvd->dsi_pack.dsi_gi.vobu_c_idn );
1147
1148     intf_WarnMsg( 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d", 
1149             p_dvd->dsi_pack.sml_pbi.category,
1150             p_dvd->dsi_pack.sml_pbi.ilvu_ea,
1151             p_dvd->dsi_pack.sml_pbi.ilvu_sa,
1152             p_dvd->dsi_pack.sml_pbi.size );
1153
1154     intf_WarnMsg( 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1155             p_dvd->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1156             p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle - 1 ].address,
1157             p_dvd->dsi_pack.sml_agli.data[ p_dvd->i_angle ].address);
1158 #endif
1159 }
1160
1161 /*****************************************************************************
1162  * DvdReadFindCell
1163  *****************************************************************************/
1164 static void DvdReadFindCell( thread_dvd_data_t * p_dvd )
1165 {
1166     int         pgc_id, pgn;
1167     int         i = 0;
1168     pgc_t *     p_pgc;
1169 #define cell p_dvd->p_cur_pgc->cell_playback
1170     if( cell[p_dvd->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1171     {
1172 #if 0
1173         p_dvd->i_next_cell = p_dvd->i_cur_cell + p_dvd->i_angle_nb;
1174         p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1175 #else
1176         p_dvd->i_cur_cell += p_dvd->i_angle - 1;
1177
1178         while( cell[p_dvd->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1179         {
1180             i++;
1181         }
1182         p_dvd->i_next_cell = p_dvd->i_cur_cell + i + 1;
1183 #endif
1184     }
1185     else
1186     {
1187         p_dvd->i_next_cell = p_dvd->i_cur_cell + 1;
1188     }
1189 #undef cell
1190     pgc_id = p_dvd->p_vts_file->vts_ptt_srpt->title[
1191                 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgcn;
1192     pgn = p_dvd->p_vts_file->vts_ptt_srpt->title[
1193                 p_dvd->i_ttn-1].ptt[p_dvd->i_chapter-1].pgn;
1194     p_pgc = p_dvd->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1195
1196     if( p_pgc->program_map[pgn-1] <= p_dvd->i_cur_cell )
1197     {
1198         p_dvd->i_chapter++;
1199         p_dvd->b_eoc = 1;
1200     }
1201 }