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