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