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