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