]> git.sesse.net Git - vlc/blob - modules/access/dvdread.c
* gui/wxwindows/open.cpp: removed deprecated DVD entries.
[vlc] / modules / access / dvdread.c
1 /*****************************************************************************
2  * dvdread.c : DvdRead input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Stéphane Borel <stef@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdio.h>
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <string.h>                                              /* strdup() */
31
32 #include <vlc/vlc.h>
33 #include <vlc/input.h>
34
35 #include "iso_lang.h"
36
37 #include "../demux/ps.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 <sys/stat.h>
46 #include <string.h>
47
48 #include <dvdread/dvd_reader.h>
49 #include <dvdread/ifo_types.h>
50 #include <dvdread/ifo_read.h>
51 #include <dvdread/nav_read.h>
52 #include <dvdread/nav_print.h>
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 #define CACHING_TEXT N_("caching value in ms")
58 #define CACHING_LONGTEXT N_( \
59     "Allows you to modify the default caching value for DVDread streams. " \
60     "This value should be set in millisecond units." )
61
62 #define CSSMETHOD_TEXT N_("Method used by libdvdcss for decryption")
63 #define CSSMETHOD_LONGTEXT N_( \
64     "Set the method used by libdvdcss for key decryption.\n" \
65     "title: decrypted title key is guessed from the encrypted sectors of " \
66            "the stream. Thus it should work with a file as well as the " \
67            "DVD device. But it sometimes takes much time to decrypt a title " \
68            "key and may even fail. With this method, the key is only checked "\
69            "at the beginning of each title, so it won't work if the key " \
70            "changes in the middle of a title.\n" \
71     "disc: the disc key is first cracked, then all title keys can be " \
72            "decrypted instantly, which allows us to check them often.\n" \
73     "key: the same as \"disc\" if you don't have a file with player keys " \
74            "at compilation time. If you do, the decryption of the disc key " \
75            "will be faster with this method. It is the one that was used by " \
76            "libcss.\n" \
77     "The default method is: key.")
78
79 static char *psz_css_list[] = { "title", "disc", "key" };
80 static char *psz_css_list_text[] = { N_("title"), N_("Disc"), N_("Key") };
81
82 static int  Open ( vlc_object_t * );
83 static void Close( vlc_object_t * );
84
85 vlc_module_begin();
86     set_description( _("DVDRead Input") );
87     add_integer( "dvdread-caching", DEFAULT_PTS_DELAY / 1000, NULL,
88         CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
89     add_string( "dvdread-css-method", NULL, NULL, CSSMETHOD_TEXT,
90                 CSSMETHOD_LONGTEXT, VLC_TRUE );
91         change_string_list( psz_css_list, psz_css_list_text, 0 );
92     set_capability( "access_demux", 0 );
93     //add_shortcut( "dvd" );
94     add_shortcut( "dvdread" );
95     add_shortcut( "dvdsimple" );
96     set_callbacks( Open, Close );
97 vlc_module_end();
98
99 /* how many blocks DVDRead will read in each loop */
100 #define DVD_BLOCK_READ_ONCE 64
101
102 /*****************************************************************************
103  * Local prototypes
104  *****************************************************************************/
105
106 struct demux_sys_t
107 {
108     /* DVDRead state */
109     dvd_reader_t *p_dvdread;
110     dvd_file_t   *p_title;
111
112     ifo_handle_t *p_vmg_file;
113     ifo_handle_t *p_vts_file;
114
115     unsigned int i_title;
116     unsigned int i_chapter, i_chapters;
117     unsigned int i_angle, i_angles;
118
119     tt_srpt_t    *p_tt_srpt;
120     pgc_t        *p_cur_pgc;
121
122     dsi_t        dsi_pack;
123
124     int          i_ttn;
125
126     unsigned int i_pack_len;
127     unsigned int i_cur_block;
128     unsigned int i_next_vobu;
129
130     /* Current title start/end blocks */
131     unsigned int i_title_start_block;
132     unsigned int i_title_end_block;
133     unsigned int i_title_blocks;
134     unsigned int i_title_offset;
135
136     unsigned int i_title_start_cell;
137     unsigned int i_cur_cell;
138     unsigned int i_next_cell;
139
140     /* track */
141     ps_track_t  tk[PS_TK_COUNT];
142
143     /* for spu variables */
144     input_thread_t *p_input;
145
146     /* FIXME */
147     uint8_t     alpha[4];
148     uint32_t    clut[16];
149
150     /* */
151     int i_aspect;
152
153     int           i_titles;
154     input_title_t **titles;
155 };
156
157 static char *ParseCL( vlc_object_t *, char *, vlc_bool_t, int *, int *, int *);
158
159 static int Control   ( demux_t *, int, va_list );
160 static int Demux     ( demux_t * );
161 static int DemuxBlock( demux_t *, uint8_t *, int );
162
163 enum
164 {
165     AR_SQUARE_PICTURE = 1,                          /* square pixels */
166     AR_3_4_PICTURE    = 2,                       /* 3:4 picture (TV) */
167     AR_16_9_PICTURE   = 3,             /* 16:9 picture (wide screen) */
168     AR_221_1_PICTURE  = 4,                 /* 2.21:1 picture (movie) */
169 };
170
171 static void DemuxTitles( demux_t *, int *, int *, int * );
172 static void ESNew( demux_t *, int, int );
173
174 static int  DvdReadSetArea  ( demux_t *, int, int, int );
175 static void DvdReadSeek     ( demux_t *, int );
176 static void DvdReadHandleDSI( demux_t *, uint8_t * );
177 static void DvdReadFindCell ( demux_t * );
178
179 /*****************************************************************************
180  * Open:
181  *****************************************************************************/
182 static int Open( vlc_object_t *p_this )
183 {
184     demux_t      *p_demux = (demux_t*)p_this;
185     demux_sys_t  *p_sys;
186     int          i_title, i_chapter, i_angle;
187     char         *psz_name;
188     char         *psz_dvdcss_env;
189     dvd_reader_t *p_dvdread;
190     ifo_handle_t *p_vmg_file;
191
192     psz_name = ParseCL( VLC_OBJECT(p_demux), p_demux->psz_path, VLC_TRUE,
193                         &i_title, &i_chapter, &i_angle );
194     if( !psz_name )
195     {
196         return VLC_EGENERIC;
197     }
198
199     /* Override environment variable DVDCSS_METHOD with config option
200      * (FIXME: this creates a small memory leak) */
201     psz_dvdcss_env = config_GetPsz( p_demux, "dvdread-css-method" );
202     if( psz_dvdcss_env && *psz_dvdcss_env )
203     {
204         char *psz_env;
205
206         psz_env = malloc( strlen("DVDCSS_METHOD=") +
207                           strlen( psz_dvdcss_env ) + 1 );
208
209         if( !psz_env )
210         {
211             free( psz_dvdcss_env );
212             return VLC_ENOMEM;
213         }
214
215         sprintf( psz_env, "%s%s", "DVDCSS_METHOD=", psz_dvdcss_env );
216
217         putenv( psz_env );
218     }
219     if( psz_dvdcss_env ) free( psz_dvdcss_env );
220
221     /* Open dvdread */
222     if( !(p_dvdread = DVDOpen( psz_name )) )
223     {
224         msg_Err( p_demux, "DVDRead cannot open source: %s", psz_name );
225         free( psz_name );
226         return VLC_EGENERIC;
227     }
228     free( psz_name );
229
230     /* Ifo allocation & initialisation */
231     if( !( p_vmg_file = ifoOpen( p_dvdread, 0 ) ) )
232     {
233         msg_Warn( p_demux, "cannot open VMG info" );
234         return VLC_EGENERIC;
235     }
236     msg_Dbg( p_demux, "VMG opened" );
237
238     /* Fill p_demux field */
239     p_demux->pf_demux = Demux;
240     p_demux->pf_control = Control;
241     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
242     memset( p_sys, 0, sizeof( demux_sys_t ) );
243
244     ps_track_init( p_sys->tk );
245     p_sys->i_aspect = -1;
246
247     p_sys->p_dvdread = p_dvdread;
248     p_sys->p_vmg_file = p_vmg_file;
249     p_sys->p_title = NULL;
250     p_sys->p_vts_file = NULL;
251
252     p_sys->i_title = p_sys->i_chapter = -1;
253     p_sys->i_angle = i_angle;
254
255     DemuxTitles( p_demux, &i_title, &i_chapter, &i_angle );
256
257     DvdReadSetArea( p_demux, i_title - 1, i_chapter, i_angle );
258
259     /* Update default_pts to a suitable value for dvdread access */
260     var_Create( p_demux, "dvdread-caching",
261                 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
262
263     return VLC_SUCCESS;
264 }
265
266 /*****************************************************************************
267  * Close:
268  *****************************************************************************/
269 static void Close( vlc_object_t *p_this )
270 {
271     demux_t     *p_demux = (demux_t*)p_this;
272     demux_sys_t *p_sys = p_demux->p_sys;
273     int i;
274
275     for( i = 0; i < PS_TK_COUNT; i++ )
276     {
277         ps_track_t *tk = &p_sys->tk[i];
278         if( tk->b_seen )
279         {
280             es_format_Clean( &tk->fmt );
281             if( tk->es ) es_out_Del( p_demux->out, tk->es );
282         }
283     }
284
285     /* Close libdvdread */
286     if( p_sys->p_title ) DVDCloseFile( p_sys->p_title );
287     if( p_sys->p_vts_file ) ifoClose( p_sys->p_vts_file );
288     if( p_sys->p_vmg_file ) ifoClose( p_sys->p_vmg_file );
289     DVDClose( p_sys->p_dvdread );
290
291     free( p_sys );
292 }
293
294 /*****************************************************************************
295  * Control:
296  *****************************************************************************/
297 static int Control( demux_t *p_demux, int i_query, va_list args )
298 {
299     demux_sys_t *p_sys = p_demux->p_sys;
300     double f, *pf;
301     vlc_bool_t *pb;
302     int64_t *pi64;
303     input_title_t ***ppp_title;
304     int *pi_int;
305     int i;
306
307     switch( i_query )
308     {
309         case DEMUX_GET_POSITION:
310         {
311             pf = (double*) va_arg( args, double* );
312
313             if( p_sys->i_title_blocks > 0 )
314                 *pf = (double)p_sys->i_title_offset / p_sys->i_title_blocks;
315             else
316                 *pf = 0.0;
317
318             return VLC_SUCCESS;
319         }
320         case DEMUX_SET_POSITION:
321         {
322             f = (double)va_arg( args, double );
323
324             DvdReadSeek( p_demux, f * p_sys->i_title_blocks );
325
326             return VLC_SUCCESS;
327         }
328
329         /* Special for access_demux */
330         case DEMUX_CAN_PAUSE:
331         case DEMUX_CAN_CONTROL_PACE:
332             /* TODO */
333             pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
334             *pb = VLC_TRUE;
335             return VLC_SUCCESS;
336
337         case DEMUX_SET_PAUSE_STATE:
338             return VLC_SUCCESS;
339
340         case DEMUX_GET_TITLE_INFO:
341             ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
342             pi_int    = (int*)va_arg( args, int* );
343
344             /* Duplicate title infos */
345             *pi_int = p_sys->i_titles;
346             *ppp_title = malloc( sizeof(input_title_t **) * p_sys->i_titles );
347             for( i = 0; i < p_sys->i_titles; i++ )
348             {
349                 (*ppp_title)[i] = vlc_input_title_Duplicate(p_sys->titles[i]);
350             }
351             return VLC_SUCCESS;
352
353         case DEMUX_SET_TITLE:
354             i = (int)va_arg( args, int );
355             if( DvdReadSetArea( p_demux, i, 0, -1 ) != VLC_SUCCESS )
356             {
357                 msg_Warn( p_demux, "cannot set title/chapter" );
358                 return VLC_EGENERIC;
359             }
360             p_demux->info.i_update |=
361                 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
362             p_demux->info.i_title = i;
363             p_demux->info.i_seekpoint = 0;
364             return VLC_SUCCESS;
365
366         case DEMUX_SET_SEEKPOINT:
367             i = (int)va_arg( args, int );
368             if( DvdReadSetArea( p_demux, -1, i, -1 ) != VLC_SUCCESS )
369             {
370                 msg_Warn( p_demux, "cannot set title/chapter" );
371                 return VLC_EGENERIC;
372             }
373             p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
374             p_demux->info.i_seekpoint = i;
375             return VLC_SUCCESS;
376
377         case DEMUX_GET_PTS_DELAY:
378             pi64 = (int64_t*)va_arg( args, int64_t * );
379             *pi64 = (int64_t)var_GetInteger( p_demux, "dvdread-caching" )*1000;
380             return VLC_SUCCESS;
381
382         /* TODO implement others */
383         default:
384             return VLC_EGENERIC;
385     }
386 }
387
388 /*****************************************************************************
389  * Demux:
390  *****************************************************************************/
391 static int Demux( demux_t *p_demux )
392 {
393     demux_sys_t *p_sys = p_demux->p_sys;
394
395     uint8_t p_buffer[DVD_VIDEO_LB_LEN * DVD_BLOCK_READ_ONCE];
396     int i_blocks_once, i_read;
397     int i;
398
399     /*
400      * Playback by cell in this pgc, starting at the cell for our chapter.
401      */
402
403     /*
404      * Check end of pack, and select the following one
405      */
406     if( !p_sys->i_pack_len )
407     {
408         /* Read NAV packet */
409         if( DVDReadBlocks( p_sys->p_title, p_sys->i_next_vobu,
410                            1, p_buffer ) != 1 )
411         {
412             msg_Err( p_demux, "read failed for block %d", p_sys->i_next_vobu );
413             return -1;
414         }
415
416         /* Basic check to be sure we don't have a empty title
417          * go to next title if so */
418         //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
419
420         /* Parse the contained dsi packet */
421         DvdReadHandleDSI( p_demux, p_buffer );
422
423         /* End of title */
424         if( p_sys->i_next_vobu > p_sys->i_title_end_block )
425         {
426             if( p_sys->i_title + 1 >= p_sys->i_titles ) return 0; /* EOF */
427
428             DvdReadSetArea( p_demux, p_sys->i_title + 1, 0, -1 );
429         }
430
431         if( p_sys->i_pack_len <= 0 )
432         {
433             msg_Err( p_demux, "i_pack_len <= 0. This shouldn't happen!" );
434             return 0; /* EOF */
435         }
436
437         /* FIXME: Ugly kludge: we send the pack block to the input for it
438          * sometimes has a zero scr and restart the sync */
439         p_sys->i_cur_block++;
440         p_sys->i_title_offset++;
441
442         DemuxBlock( p_demux, p_buffer, DVD_VIDEO_LB_LEN );
443     }
444
445     if( p_sys->i_cur_block > p_sys->i_title_end_block )
446     {
447         if( p_sys->i_title + 1 >= p_sys->i_titles ) return 0; /* EOF */
448
449         DvdReadSetArea( p_demux, p_sys->i_title + 1, 0, -1 );
450     }
451
452     /*
453      * Read actual data
454      */
455     i_blocks_once = __MIN( p_sys->i_pack_len, DVD_BLOCK_READ_ONCE );
456     p_sys->i_pack_len -= i_blocks_once;
457
458     /* Reads from DVD */
459     i_read = DVDReadBlocks( p_sys->p_title, p_sys->i_cur_block,
460                             i_blocks_once, p_buffer );
461     if( i_read != i_blocks_once )
462     {
463         msg_Err( p_demux, "read failed for %d/%d blocks at 0x%02x",
464                  i_read, i_blocks_once, p_sys->i_cur_block );
465         return -1;
466     }
467
468     p_sys->i_cur_block += i_read;
469     p_sys->i_title_offset += i_read;
470
471 #if 0
472     msg_Dbg( p_demux, "i_blocks: %d len: %d current: 0x%02x",
473              i_read, p_sys->i_pack_len, p_sys->i_cur_block );
474 #endif
475
476     for( i = 0; i < i_read; i++ )
477     {
478         DemuxBlock( p_demux, p_buffer + i * DVD_VIDEO_LB_LEN,
479                     DVD_VIDEO_LB_LEN );
480     }
481
482 #undef p_pgc
483
484     return 1;
485 }
486
487 /*****************************************************************************
488  * DemuxBlock: demux a given block
489  *****************************************************************************/
490 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
491 {
492     demux_sys_t *p_sys = p_demux->p_sys;
493     uint8_t     *p = pkt;
494
495     while( p < &pkt[i_pkt] )
496     {
497         int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
498         block_t *p_pkt;
499         if( i_size <= 0 )
500         {
501             break;
502         }
503
504         /* Create a block */
505         p_pkt = block_New( p_demux, i_size );
506         memcpy( p_pkt->p_buffer, p, i_size);
507
508         /* Parse it and send it */
509         switch( 0x100 | p[3] )
510         {
511         case 0x1b9:
512         case 0x1bb:
513         case 0x1bc:
514
515 #ifdef DVDREAD_DEBUG
516             if( p[3] == 0xbc )
517             {
518                 msg_Warn( p_demux, "received a PSM packet" );
519             }
520             else if( p[3] == 0xbb )
521             {
522                 msg_Warn( p_demux, "received a SYSTEM packet" );
523             }
524 #endif
525             block_Release( p_pkt );
526             break;
527
528         case 0x1ba:
529         {
530             int64_t i_scr;
531             int i_mux_rate;
532             if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
533             {
534                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
535             }
536             block_Release( p_pkt );
537             break;
538         }
539         default:
540         {
541             int i_id = ps_pkt_id( p_pkt );
542             if( i_id >= 0xc0 )
543             {
544                 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
545
546                 if( !tk->b_seen )
547                 {
548                     ESNew( p_demux, i_id, 0 );
549                 }
550                 if( tk->b_seen && tk->es &&
551                     !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
552                 {
553                     es_out_Send( p_demux->out, tk->es, p_pkt );
554                 }
555                 else
556                 {
557                     block_Release( p_pkt );
558                 }
559             }
560             else
561             {
562                 block_Release( p_pkt );
563             }
564             break;
565         }
566         }
567
568         p += i_size;
569     }
570
571     return VLC_SUCCESS;
572 }
573
574 /*****************************************************************************
575  * ESNew: register a new elementary stream
576  *****************************************************************************/
577 static void ESNew( demux_t *p_demux, int i_id, int i_lang )
578 {
579     demux_sys_t *p_sys = p_demux->p_sys;
580     ps_track_t  *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
581     char psz_language[3];
582
583     if( tk->b_seen ) return;
584
585     if( ps_track_fill( tk, i_id ) )
586     {
587         msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
588         return;
589     }
590
591     psz_language[0] = psz_language[1] = psz_language[2] = 0;
592     if( i_lang && i_lang != 0xffff )
593     {
594         psz_language[0] = (i_lang >> 8)&0xff;
595         psz_language[1] = (i_lang     )&0xff;
596     }
597
598     /* Add a new ES */
599     if( tk->fmt.i_cat == VIDEO_ES )
600     {
601         if( p_sys->i_aspect >= 0 )
602         {
603             tk->fmt.video.i_aspect = p_sys->i_aspect;
604         }
605     }
606     else if( tk->fmt.i_cat == AUDIO_ES )
607     {
608         int i_audio = -1;
609         /* find the audio number PLEASE find another way */
610         if( (i_id&0xbdf8) == 0xbd88 )       /* dts */
611         {
612             i_audio = i_id&0x07;
613         }
614         else if( (i_id&0xbdf0) == 0xbd80 )  /* a52 */
615         {
616             i_audio = i_id&0xf;
617         }
618         else if( (i_id&0xbdf0) == 0xbda0 )  /* lpcm */
619         {
620             i_audio = i_id&0x1f;
621         }
622         else if( ( i_id&0xe0 ) == 0xc0 )    /* mpga */
623         {
624             i_audio = i_id&0x1f;
625         }
626
627         if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
628     }
629     else if( tk->fmt.i_cat == SPU_ES )
630     {
631         /* Palette */
632         tk->fmt.subs.spu.palette[0] = 0xBeef;
633         memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
634                 16 * sizeof( uint32_t ) );
635
636         if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
637     }
638
639     tk->es = es_out_Add( p_demux->out, &tk->fmt );
640     tk->b_seen = VLC_TRUE;
641 }
642
643 /*****************************************************************************
644  * DvdReadSetArea: initialize input data for title x, chapter y.
645  * It should be called for each user navigation request.
646  *****************************************************************************
647  * Take care that i_title and i_chapter start from 0.
648  *****************************************************************************/
649 static int DvdReadSetArea( demux_t *p_demux, int i_title, int i_chapter,
650                            int i_angle )
651 {
652     demux_sys_t *p_sys = p_demux->p_sys;
653     int         pgc_id = 0;
654     int         pgn = 0;
655     int         i;
656     vlc_value_t val;
657
658 #define p_pgc p_sys->p_cur_pgc
659 #define p_vmg p_sys->p_vmg_file
660 #define p_vts p_sys->p_vts_file
661
662     if( i_title >= 0 && i_title < p_sys->i_titles &&
663         i_title != p_sys->i_title )
664     {
665         int i_start_cell, i_end_cell;
666
667         if( p_sys->p_title != NULL ) DVDCloseFile( p_sys->p_title );
668         if( p_vts != NULL ) ifoClose( p_vts );
669         p_sys->i_title = i_title;
670
671         /*
672          *  We have to load all title information
673          */
674         msg_Dbg( p_demux, "open VTS %d, for title %d",
675                  p_vmg->tt_srpt->title[i_title].title_set_nr, i_title + 1 );
676
677         /* Ifo vts */
678         if( !( p_vts = ifoOpen( p_sys->p_dvdread,
679                p_vmg->tt_srpt->title[i_title].title_set_nr ) ) )
680         {
681             msg_Err( p_demux, "fatal error in vts ifo" );
682             return VLC_EGENERIC;
683         }
684
685         /* Title position inside the selected vts */
686         p_sys->i_ttn = p_vmg->tt_srpt->title[i_title].vts_ttn;
687
688         /* Find title start/end */
689         pgc_id = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgcn;
690         pgn = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgn;
691         p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
692
693         p_sys->i_title_start_cell =
694             i_start_cell = p_pgc->program_map[pgn - 1] - 1;
695         p_sys->i_title_start_block =
696             p_pgc->cell_playback[i_start_cell].first_sector;
697
698         i_end_cell = p_pgc->nr_of_cells - 1;
699         p_sys->i_title_end_block =
700             p_pgc->cell_playback[i_end_cell].last_sector;
701
702         p_sys->i_title_offset = 0;
703
704         p_sys->i_title_blocks = 0;
705         for( i = i_start_cell; i <= i_end_cell; i++ )
706         {
707             p_sys->i_title_blocks += p_pgc->cell_playback[i].last_sector -
708                 p_pgc->cell_playback[i].first_sector + 1;
709         }
710
711         msg_Dbg( p_demux, "title %d vts_title %d pgc %d pgn %d "
712                  "start %d end %d blocks: %d",
713                  i_title + 1, p_sys->i_ttn, pgc_id, pgn,
714                  p_sys->i_title_start_block, p_sys->i_title_end_block,
715                  p_sys->i_title_blocks );
716
717         /*
718          * Set properties for current chapter
719          */
720         p_sys->i_chapter = 0;
721         p_sys->i_chapters =
722             p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].nr_of_ptts;
723
724         pgc_id = p_vts->vts_ptt_srpt->title[
725                     p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgcn;
726         pgn = p_vts->vts_ptt_srpt->title[
727                     p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgn;
728
729         p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
730         p_sys->i_pack_len = 0;
731         p_sys->i_next_cell =
732             p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
733         DvdReadFindCell( p_demux );
734
735         p_sys->i_next_vobu = p_sys->i_cur_block =
736             p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
737
738         /*
739          * Angle management
740          */
741         p_sys->i_angles = p_vmg->tt_srpt->title[i_title].nr_of_angles;
742         if( p_sys->i_angle > p_sys->i_angles ) p_sys->i_angle = 1;
743
744         /*
745          * We've got enough info, time to open the title set data.
746          */
747         if( !( p_sys->p_title = DVDOpenFile( p_sys->p_dvdread,
748             p_vmg->tt_srpt->title[i_title].title_set_nr,
749             DVD_READ_TITLE_VOBS ) ) )
750         {
751             msg_Err( p_demux, "cannot open title (VTS_%02d_1.VOB)",
752                      p_vmg->tt_srpt->title[i_title].title_set_nr );
753             return VLC_EGENERIC;
754         }
755
756         //IfoPrintTitle( p_demux );
757
758         /*
759          * Destroy obsolete ES by reinitializing program 0
760          * and find all ES in title with ifo data
761          */
762         es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
763
764         for( i = 0; i < PS_TK_COUNT; i++ )
765         {
766             ps_track_t *tk = &p_sys->tk[i];
767             if( tk->b_seen )
768             {
769                 es_format_Clean( &tk->fmt );
770                 if( tk->es ) es_out_Del( p_demux->out, tk->es );
771             }
772             tk->b_seen = VLC_FALSE;
773         }
774
775         if( p_demux->info.i_title != i_title )
776         {
777             p_demux->info.i_update |=
778                 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
779             p_demux->info.i_title = i_title;
780             p_demux->info.i_seekpoint = 0;
781         }
782
783         /* TODO: re-add angles */
784
785
786         ESNew( p_demux, 0xe0, 0 ); /* Video, FIXME ? */
787
788 #define audio_control \
789     p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
790
791         /* Audio ES, in the order they appear in the .ifo */
792         for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
793         {
794             int i_position = 0;
795             uint16_t i_id;
796
797             //IfoPrintAudio( p_demux, i );
798
799             /* Audio channel is active if first byte is 0x80 */
800             if( audio_control & 0x8000 )
801             {
802                 i_position = ( audio_control & 0x7F00 ) >> 8;
803
804                 msg_Dbg( p_demux, "audio position  %d", i_position );
805                 switch( p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format )
806                 {
807                 case 0x00: /* A52 */
808                     i_id = (0x80 + i_position) | 0xbd00;
809                     break;
810                 case 0x02:
811                 case 0x03: /* MPEG audio */
812                     i_id = 0xc000 + i_position;
813                     break;
814                 case 0x04: /* LPCM */
815                     i_id = (0xa0 + i_position) | 0xbd00;
816                     break;
817                 case 0x06: /* DTS */
818                     i_id = (0x88 + i_position) | 0xbd00;
819                     break;
820                 default:
821                     i_id = 0;
822                     msg_Err( p_demux, "unknown audio type %.2x",
823                         p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format );
824                 }
825
826                 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
827                        vts_audio_attr[i - 1].lang_code );
828             }
829         }
830 #undef audio_control
831
832 #define spu_control \
833     p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
834
835         /* Sub Picture ES */
836         for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
837         {
838             int i_position = 0;
839             uint16_t i_id;
840
841             //IfoPrintSpu( p_sys, i );
842             msg_Dbg( p_demux, "spu %d 0x%02x", i, spu_control );
843
844             if( spu_control & 0x80000000 )
845             {
846                 /*  there are several streams for one spu */
847                 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
848                 {
849                     /* 16:9 */
850                     switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
851                     {
852                     case 1:
853                         i_position = spu_control & 0xff;
854                         break;
855                     case 2:
856                         i_position = ( spu_control >> 8 ) & 0xff;
857                         break;
858                     default:
859                         i_position = ( spu_control >> 16 ) & 0xff;
860                         break;
861                     }
862                 }
863                 else
864                 {
865                     /* 4:3 */
866                     i_position = ( spu_control >> 24 ) & 0x7F;
867                 }
868
869                 i_id = (0x20 + i_position) | 0xbd00;
870
871                 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
872                        vts_subp_attr[i - 1].lang_code );
873             }
874         }
875 #undef spu_control
876
877     }
878     else
879     {
880         /* TODO: update input ? */
881     }
882
883     /*
884      * Chapter selection
885      */
886
887     if( i_chapter >= 0 && i_chapter <= p_sys->i_chapters &&
888         i_chapter != p_sys->i_chapter )
889     {
890         pgc_id = p_vts->vts_ptt_srpt->title[
891                      p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
892         pgn = p_vts->vts_ptt_srpt->title[
893                   p_sys->i_ttn - 1].ptt[i_chapter].pgn;
894
895         p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
896
897         p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
898         p_sys->i_chapter = i_chapter;
899         DvdReadFindCell( p_demux );
900
901         p_sys->i_title_offset = 0;
902         for( i = p_sys->i_title_start_cell; i < p_sys->i_cur_cell; i++ )
903         {
904             p_sys->i_title_offset += p_pgc->cell_playback[i].last_sector -
905                 p_pgc->cell_playback[i].first_sector + 1;
906         }
907
908         p_sys->i_pack_len = 0;
909         p_sys->i_next_vobu = p_sys->i_cur_block =
910             p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
911
912         if( p_demux->info.i_seekpoint != i_chapter )
913         {
914             p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
915             p_demux->info.i_seekpoint = i_chapter;
916         }
917     }
918
919 #undef p_pgc
920 #undef p_vts
921 #undef p_vmg
922
923     return VLC_SUCCESS;
924 }
925
926 /*****************************************************************************
927  * DvdReadSeek : Goes to a given position on the stream.
928  *****************************************************************************
929  * This one is used by the input and translate chronological position from
930  * input to logical position on the device.
931  *****************************************************************************/
932 static void DvdReadSeek( demux_t *p_demux, int i_block_offset )
933 {
934     demux_sys_t *p_sys = p_demux->p_sys;
935     unsigned int i_tmp;
936     unsigned int i_chapter = 0;
937     unsigned int i_cell = 0;
938     unsigned int i_vobu = 0;
939     unsigned int i_sub_cell = 0;
940     int i_block;
941
942 #define p_pgc p_sys->p_cur_pgc
943 #define p_vts p_sys->p_vts_file
944
945     /* Find cell */
946     p_sys->i_title_offset = i_block = i_block_offset;
947     while( i_block >= (int)p_pgc->cell_playback[i_cell].last_sector -
948            (int)p_pgc->cell_playback[i_cell].first_sector + 1 )
949     {
950         i_cell++;
951
952         i_block -= p_pgc->cell_playback[i_cell].last_sector -
953             p_pgc->cell_playback[i_cell].first_sector + 1;
954     }
955
956     i_block += p_pgc->cell_playback[i_cell].first_sector;
957
958     /* Find chapter */
959     do
960     {
961         int pgc_id, pgn;
962
963         i_chapter++;
964         pgc_id = p_vts->vts_ptt_srpt->title[
965                     p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
966         pgn = p_vts->vts_ptt_srpt->title[
967                     p_sys->i_ttn - 1].ptt[i_chapter].pgn;
968
969         i_tmp = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc->program_map[pgn-1];
970
971     } while( i_tmp <= i_cell );
972
973     /* Find vobu */
974     while( p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu] <= i_block )
975     {
976         i_vobu++;
977     }
978
979     /* Find sub_cell */
980     while( p_vts->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
981            p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
982     {
983         i_sub_cell++;
984     }
985
986 #if 1
987     msg_Dbg( p_demux, "cell %d i_sub_cell %d chapter %d vobu %d "
988              "cell_sector %d vobu_sector %d sub_cell_sector %d",
989              i_cell, i_sub_cell,i_chapter, i_vobu,
990              p_sys->p_cur_pgc->cell_playback[i_cell].first_sector,
991              p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu],
992              p_vts->vts_c_adt->cell_adr_table[i_sub_cell - 1].start_sector);
993 #endif
994
995     p_sys->i_cur_block = i_block;
996     p_sys->i_next_vobu = p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu];
997     p_sys->i_pack_len = p_sys->i_next_vobu - i_block;
998     p_sys->i_cur_cell = i_cell;
999     p_sys->i_chapter = i_chapter;
1000     DvdReadFindCell( p_demux );
1001
1002 #undef p_vts
1003 #undef p_pgc
1004
1005     return;
1006 }
1007
1008 /*****************************************************************************
1009  * DvdReadHandleDSI
1010  *****************************************************************************/
1011 static void DvdReadHandleDSI( demux_t *p_demux, uint8_t *p_data )
1012 {
1013     demux_sys_t *p_sys = p_demux->p_sys;
1014
1015     navRead_DSI( &p_sys->dsi_pack, &p_data[DSI_START_BYTE] );
1016
1017     /*
1018      * Determine where we go next.  These values are the ones we mostly
1019      * care about.
1020      */
1021     p_sys->i_cur_block = p_sys->dsi_pack.dsi_gi.nv_pck_lbn;
1022
1023     /*
1024      * If we're not at the end of this cell, we can determine the next
1025      * VOBU to display using the VOBU_SRI information section of the
1026      * DSI.  Using this value correctly follows the current angle,
1027      * avoiding the doubled scenes in The Matrix, and makes our life
1028      * really happy.
1029      */
1030     if( p_sys->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1031     {
1032         switch( ( p_sys->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1033         {
1034         case 0x4:
1035             /* Interleaved unit with no angle */
1036             if( p_sys->dsi_pack.sml_pbi.ilvu_sa != 0 )
1037             {
1038                 p_sys->i_next_vobu = p_sys->i_cur_block +
1039                     p_sys->dsi_pack.sml_pbi.ilvu_sa;
1040                 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1041             }
1042             else
1043             {
1044                 p_sys->i_next_vobu = p_sys->i_cur_block +
1045                     p_sys->dsi_pack.dsi_gi.vobu_ea + 1;
1046                 p_sys->i_pack_len = p_sys->dsi_pack.dsi_gi.vobu_ea;
1047             }
1048             break;
1049         case 0x5:
1050             /* vobu is end of ilvu */
1051             if( p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address )
1052             {
1053                 p_sys->i_next_vobu = p_sys->i_cur_block +
1054                     p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address;
1055                 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1056
1057                 break;
1058             }
1059         case 0x6:
1060             /* vobu is beginning of ilvu */
1061         case 0x9:
1062             /* next scr is 0 */
1063         case 0xa:
1064             /* entering interleaved section */
1065         case 0x8:
1066             /* non interleaved cells in interleaved section */
1067         default:
1068             p_sys->i_next_vobu = p_sys->i_cur_block +
1069                 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1070             p_sys->i_pack_len = p_sys->dsi_pack.dsi_gi.vobu_ea;
1071             break;
1072         }
1073     }
1074     else
1075     {
1076         p_sys->i_cur_cell = p_sys->i_next_cell;
1077         DvdReadFindCell( p_demux );
1078
1079         p_sys->i_pack_len = p_sys->dsi_pack.dsi_gi.vobu_ea;
1080         p_sys->i_next_vobu =
1081             p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1082     }
1083
1084 #if 0
1085     msg_Dbg( p_input, 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1086              p_sys->dsi_pack.dsi_gi.nv_pck_scr,
1087              p_sys->dsi_pack.dsi_gi.nv_pck_lbn,
1088              p_sys->dsi_pack.dsi_gi.vobu_ea,
1089              p_sys->dsi_pack.dsi_gi.vobu_vob_idn,
1090              p_sys->dsi_pack.dsi_gi.vobu_c_idn );
1091
1092     msg_Dbg( p_input, 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1093              p_sys->dsi_pack.sml_pbi.category,
1094              p_sys->dsi_pack.sml_pbi.ilvu_ea,
1095              p_sys->dsi_pack.sml_pbi.ilvu_sa,
1096              p_sys->dsi_pack.sml_pbi.size );
1097
1098     msg_Dbg( p_input, 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1099              p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1100              p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle - 1 ].address,
1101              p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle ].address);
1102 #endif
1103 }
1104
1105 /*****************************************************************************
1106  * DvdReadFindCell
1107  *****************************************************************************/
1108 static void DvdReadFindCell( demux_t *p_demux )
1109 {
1110     demux_sys_t *p_sys = p_demux->p_sys;
1111
1112     pgc_t *p_pgc;
1113     int   pgc_id, pgn;
1114     int   i = 0;
1115
1116 #define cell p_sys->p_cur_pgc->cell_playback
1117
1118     if( cell[p_sys->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1119     {
1120 #if 0
1121         p_sys->i_next_cell = p_sys->i_cur_cell + p_sys->i_angle_nb;
1122         p_sys->i_cur_cell += p_sys->i_angle - 1;
1123 #else
1124         p_sys->i_cur_cell += p_sys->i_angle - 1;
1125
1126         while( cell[p_sys->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1127         {
1128             i++;
1129         }
1130         p_sys->i_next_cell = p_sys->i_cur_cell + i + 1;
1131 #endif
1132     }
1133     else
1134     {
1135         p_sys->i_next_cell = p_sys->i_cur_cell + 1;
1136     }
1137
1138 #undef cell
1139
1140     pgc_id = p_sys->p_vts_file->vts_ptt_srpt->title[
1141                 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgcn;
1142     pgn = p_sys->p_vts_file->vts_ptt_srpt->title[
1143               p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgn;
1144     p_pgc = p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
1145
1146     if( p_pgc->program_map[pgn - 1] <= p_sys->i_cur_cell )
1147     {
1148         p_sys->i_chapter++;
1149
1150         if( p_sys->i_chapter < p_sys->i_chapters &&
1151             p_demux->info.i_seekpoint != p_sys->i_chapter )
1152         {
1153             p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1154             p_demux->info.i_seekpoint = p_sys->i_chapter;
1155         }
1156     }
1157 }
1158
1159 /*****************************************************************************
1160  * DemuxTitles: get the titles/chapters structure
1161  *****************************************************************************/
1162 static void DemuxTitles( demux_t *p_demux,
1163                          int *pi_title, int *pi_chapter, int *pi_angle )
1164 {
1165     demux_sys_t *p_sys = p_demux->p_sys;
1166     input_title_t *t;
1167     seekpoint_t *s;
1168     int32_t i_titles;
1169     int i;
1170
1171     /* Find out number of titles/chapters */
1172 #define tt_srpt p_sys->p_vmg_file->tt_srpt
1173
1174     i_titles = tt_srpt->nr_of_srpts;
1175     msg_Dbg( p_demux, "number of titles: %d", i_titles );
1176
1177     for( i = 0; i < i_titles; i++ )
1178     {
1179         int32_t i_chapters = 0;
1180         int j;
1181
1182         i_chapters = tt_srpt->title[i].nr_of_ptts;
1183         msg_Dbg( p_demux, "title %d has %d chapters", i, i_chapters );
1184
1185         t = vlc_input_title_New();
1186         for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
1187         {
1188             s = vlc_seekpoint_New();
1189             TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1190         }
1191
1192         TAB_APPEND( p_sys->i_titles, p_sys->titles, t );
1193     }
1194
1195     /* Set forced title/chapter/angle */
1196     *pi_title = (*pi_title >= 1 && *pi_title <= i_titles) ? *pi_title : 1;
1197     *pi_chapter = (*pi_chapter >= 0 && *pi_chapter <
1198         tt_srpt->title[*pi_title].nr_of_ptts) ? *pi_chapter : 0;
1199
1200 #undef tt_srpt
1201 }
1202
1203 /*****************************************************************************
1204  * ParseCL: parse command line
1205  *****************************************************************************/
1206 static char *ParseCL( vlc_object_t *p_this, char *psz_name, vlc_bool_t b_force,
1207                       int *i_title, int *i_chapter, int *i_angle )
1208 {
1209     char *psz_parser, *psz_source, *psz_next;
1210
1211     psz_source = strdup( psz_name );
1212     if( psz_source == NULL ) return NULL;
1213
1214     *i_title = 0;
1215     *i_chapter = 0;
1216     *i_angle = 1;
1217
1218     /* Start with the end, because you could have :
1219      * dvdnav:/Volumes/my@toto/VIDEO_TS@1,1
1220      * (yes, this is kludgy). */
1221     for( psz_parser = psz_source + strlen(psz_source) - 1;
1222          psz_parser >= psz_source && *psz_parser != '@';
1223          psz_parser-- );
1224
1225     if( psz_parser >= psz_source && *psz_parser == '@' )
1226     {
1227         /* Found options */
1228         *psz_parser = '\0';
1229         ++psz_parser;
1230
1231         *i_title = (int)strtol( psz_parser, &psz_next, 10 );
1232         if( *psz_next )
1233         {
1234             psz_parser = psz_next + 1;
1235             *i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
1236             if( *psz_next )
1237             {
1238                 *i_angle = (int)strtol( psz_next + 1, NULL, 10 );
1239             }
1240         }
1241     }
1242
1243     *i_title   = *i_title >= 0 ? *i_title : 0;
1244     *i_chapter = *i_chapter    ? *i_chapter : 0;
1245     *i_angle   = *i_angle      ? *i_angle : 1;
1246
1247     if( !*psz_source )
1248     {
1249         free( psz_source );
1250         if( !b_force )
1251         {
1252             return NULL;
1253         }
1254         psz_source = config_GetPsz( p_this, "dvd" );
1255         if( !psz_source ) return NULL;
1256     }
1257
1258 #ifdef WIN32
1259     if( psz_source[0] && psz_source[1] == ':' &&
1260         psz_source[2] == '\\' && psz_source[3] == '\0' )
1261     {
1262         psz_source[2] = '\0';
1263     }
1264 #endif
1265
1266     msg_Dbg( p_this, "dvdroot=%s title=%d chapter=%d angle=%d",
1267              psz_source, *i_title, *i_chapter, *i_angle );
1268
1269     return psz_source;
1270 }