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