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