1 /*****************************************************************************
2 * dvdread.c : DvdRead input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2006 VLC authors and VideoLAN
7 * Authors: Stéphane Borel <stef@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * NOTA BENE: this module requires the linking against a library which is
27 * known to require licensing under the GNU General Public License version 2
28 * (or later). Therefore, the result of compiling this module will normally
29 * be subject to the terms of that later license.
30 *****************************************************************************/
33 /*****************************************************************************
35 *****************************************************************************/
41 #include <vlc_common.h>
42 #include <vlc_plugin.h>
43 #include <vlc_input.h>
44 #include <vlc_access.h>
45 #include <vlc_charset.h>
46 #include <vlc_interface.h>
47 #include <vlc_dialog.h>
49 #include <vlc_iso_lang.h>
51 #include "../demux/mpeg/pes.h"
52 #include "../demux/mpeg/ps.h"
54 #include <sys/types.h>
57 #include <dvdread/dvd_reader.h>
58 #include <dvdread/ifo_types.h>
59 #include <dvdread/ifo_read.h>
60 #include <dvdread/nav_read.h>
61 #include <dvdread/nav_print.h>
65 /*****************************************************************************
67 *****************************************************************************/
68 #define ANGLE_TEXT N_("DVD angle")
69 #define ANGLE_LONGTEXT N_( \
70 "Default DVD angle." )
72 static int Open ( vlc_object_t
* );
73 static void Close( vlc_object_t
* );
76 set_shortname( N_("DVD without menus") )
77 set_description( N_("DVDRead Input (no menu support)") )
78 set_category( CAT_INPUT
)
79 set_subcategory( SUBCAT_INPUT_ACCESS
)
80 add_integer( "dvdread-angle", 1, ANGLE_TEXT
,
81 ANGLE_LONGTEXT
, false )
82 add_obsolete_string( "dvdread-css-method" ) /* obsolete since 1.1.0 */
83 set_capability( "access_demux", 0 )
84 add_shortcut( "dvd", "dvdread", "dvdsimple" )
85 set_callbacks( Open
, Close
)
88 /* how many blocks DVDRead will read in each loop */
89 #define DVD_BLOCK_READ_ONCE 4
91 /*****************************************************************************
93 *****************************************************************************/
98 dvd_reader_t
*p_dvdread
;
101 ifo_handle_t
*p_vmg_file
;
102 ifo_handle_t
*p_vts_file
;
105 int i_chapter
, i_chapters
;
106 int i_angle
, i_angles
;
108 tt_srpt_t
*p_tt_srpt
;
119 /* Current title start/end blocks */
120 int i_title_start_block
;
121 int i_title_end_block
;
124 mtime_t i_title_cur_time
;
126 int i_title_start_cell
;
127 int i_title_end_cell
;
130 mtime_t i_cell_cur_time
;
131 mtime_t i_cell_duration
;
134 ps_track_t tk
[PS_TK_COUNT
];
137 input_title_t
**titles
;
147 static int Control ( demux_t
*, int, va_list );
148 static int Demux ( demux_t
* );
149 static int DemuxBlock( demux_t
*, const uint8_t *, int );
151 static void DemuxTitles( demux_t
*, int * );
152 static void ESNew( demux_t
*, int, int );
154 static int DvdReadSetArea ( demux_t
*, int, int, int );
155 static void DvdReadSeek ( demux_t
*, int );
156 static void DvdReadHandleDSI( demux_t
*, uint8_t * );
157 static void DvdReadFindCell ( demux_t
* );
159 /*****************************************************************************
161 *****************************************************************************/
162 static int Open( vlc_object_t
*p_this
)
164 demux_t
*p_demux
= (demux_t
*)p_this
;
167 ifo_handle_t
*p_vmg_file
;
169 if( !p_demux
->psz_file
|| !*p_demux
->psz_file
)
171 /* Only when selected */
172 if( !*p_demux
->psz_access
)
175 psz_file
= var_InheritString( p_this
, "dvd" );
178 psz_file
= strdup( p_demux
->psz_file
);
180 #if defined( _WIN32 ) || defined( __OS2__ )
181 if( psz_file
!= NULL
)
183 size_t flen
= strlen( psz_file
);
184 if( flen
> 0 && psz_file
[flen
- 1] == '\\' )
185 psz_file
[flen
- 1] = '\0';
188 psz_file
= strdup("");
190 if( unlikely(psz_file
== NULL
) )
194 const char *psz_path
= ToLocale( psz_file
);
195 dvd_reader_t
*p_dvdread
= DVDOpen( psz_path
);
197 LocaleFree( psz_path
);
198 if( p_dvdread
== NULL
)
200 msg_Err( p_demux
, "DVDRead cannot open source: %s", psz_file
);
201 vlc_dialog_display_error( p_demux
, _("Playback failure"),
202 _("DVDRead could not open the disc \"%s\"."), psz_file
);
208 /* Ifo allocation & initialisation */
209 if( !( p_vmg_file
= ifoOpen( p_dvdread
, 0 ) ) )
212 if( DVDUDFVolumeInfo( p_dvdread
, rgsz_volid
, 32, NULL
, 0 ) )
214 if( DVDISOVolumeInfo( p_dvdread
, rgsz_volid
, 32, NULL
, 0 ) == 0 )
216 vlc_dialog_display_error( p_demux
, _("Playback failure"),
217 _("Cannot play a non-UDF mastered DVD." ) );
218 msg_Err( p_demux
, "Invalid UDF DVD. (Found ISO9660 '%s')", rgsz_volid
);
221 msg_Warn( p_demux
, "cannot open VMG info" );
224 msg_Dbg( p_demux
, "VMG opened" );
226 /* Fill p_demux field */
227 DEMUX_INIT_COMMON(); p_sys
= p_demux
->p_sys
;
229 ps_track_init( p_sys
->tk
);
230 p_sys
->i_sar_num
= 0;
231 p_sys
->i_sar_den
= 0;
232 p_sys
->i_title_cur_time
= (mtime_t
) 0;
233 p_sys
->i_cell_cur_time
= (mtime_t
) 0;
234 p_sys
->i_cell_duration
= (mtime_t
) 0;
236 p_sys
->p_dvdread
= p_dvdread
;
237 p_sys
->p_vmg_file
= p_vmg_file
;
238 p_sys
->p_title
= NULL
;
239 p_sys
->p_vts_file
= NULL
;
241 p_sys
->i_title
= p_sys
->i_chapter
= -1;
242 p_sys
->i_mux_rate
= 0;
244 p_sys
->i_angle
= var_CreateGetInteger( p_demux
, "dvdread-angle" );
245 if( p_sys
->i_angle
<= 0 ) p_sys
->i_angle
= 1;
247 DemuxTitles( p_demux
, &p_sys
->i_angle
);
248 if( DvdReadSetArea( p_demux
, 0, 0, p_sys
->i_angle
) != VLC_SUCCESS
)
250 msg_Err( p_demux
, "DvdReadSetArea(0,0,%i) failed (can't decrypt DVD?)",
259 /*****************************************************************************
261 *****************************************************************************/
262 static void Close( vlc_object_t
*p_this
)
264 demux_t
*p_demux
= (demux_t
*)p_this
;
265 demux_sys_t
*p_sys
= p_demux
->p_sys
;
267 for( int i
= 0; i
< PS_TK_COUNT
; i
++ )
269 ps_track_t
*tk
= &p_sys
->tk
[i
];
270 if( tk
->b_configured
)
272 es_format_Clean( &tk
->fmt
);
273 if( tk
->es
) es_out_Del( p_demux
->out
, tk
->es
);
277 /* Free the array of titles */
278 for( int i
= 0; i
< p_sys
->i_titles
; i
++ )
279 vlc_input_title_Delete( p_sys
->titles
[i
] );
280 TAB_CLEAN( p_sys
->i_titles
, p_sys
->titles
);
282 /* Close libdvdread */
283 if( p_sys
->p_title
) DVDCloseFile( p_sys
->p_title
);
284 if( p_sys
->p_vts_file
) ifoClose( p_sys
->p_vts_file
);
285 if( p_sys
->p_vmg_file
) ifoClose( p_sys
->p_vmg_file
);
286 DVDClose( p_sys
->p_dvdread
);
291 static int64_t dvdtime_to_time( dvd_time_t
*dtime
, uint8_t still_time
)
293 /* Macro to convert Binary Coded Decimal to Decimal */
294 #define BCD2D(__x__) (((__x__ & 0xf0) >> 4) * 10 + (__x__ & 0x0f))
297 int64_t i_micro_second
= 0;
299 if (still_time
== 0 || still_time
== 0xFF)
301 i_micro_second
+= (int64_t)(BCD2D(dtime
->hour
)) * 60 * 60 * 1000000;
302 i_micro_second
+= (int64_t)(BCD2D(dtime
->minute
)) * 60 * 1000000;
303 i_micro_second
+= (int64_t)(BCD2D(dtime
->second
)) * 1000000;
305 switch((dtime
->frame_u
& 0xc0) >> 6)
317 f_ms
= BCD2D(dtime
->frame_u
&0x3f) * 1000.0 / f_fps
;
318 i_micro_second
+= (int64_t)(f_ms
* 1000.0);
322 i_micro_second
= still_time
;
323 i_micro_second
= (int64_t)((double)i_micro_second
* 1000000.0);
326 return i_micro_second
;
329 /*****************************************************************************
331 *****************************************************************************/
332 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
334 demux_sys_t
*p_sys
= p_demux
->p_sys
;
338 input_title_t
***ppp_title
;
344 case DEMUX_GET_POSITION
:
346 pf
= va_arg( args
, double * );
348 if( p_sys
->i_title_blocks
> 0 )
349 *pf
= (double)p_sys
->i_title_offset
/ p_sys
->i_title_blocks
;
355 case DEMUX_SET_POSITION
:
357 f
= va_arg( args
, double );
359 DvdReadSeek( p_demux
, f
* p_sys
->i_title_blocks
);
364 pi64
= va_arg( args
, int64_t * );
365 if( p_demux
->info
.i_title
>= 0 && p_demux
->info
.i_title
< p_sys
->i_titles
)
367 *pi64
= (int64_t) dvdtime_to_time( &p_sys
->p_cur_pgc
->playback_time
, 0 ) /
368 p_sys
->i_title_blocks
* p_sys
->i_title_offset
;
374 case DEMUX_GET_LENGTH
:
375 pi64
= va_arg( args
, int64_t * );
376 if( p_demux
->info
.i_title
>= 0 && p_demux
->info
.i_title
< p_sys
->i_titles
)
378 *pi64
= (int64_t)dvdtime_to_time( &p_sys
->p_cur_pgc
->playback_time
, 0 );
384 /* Special for access_demux */
385 case DEMUX_CAN_PAUSE
:
387 case DEMUX_CAN_CONTROL_PACE
:
389 pb
= va_arg( args
, bool * );
393 case DEMUX_SET_PAUSE_STATE
:
396 case DEMUX_GET_TITLE_INFO
:
397 ppp_title
= va_arg( args
, input_title_t
*** );
398 pi_int
= va_arg( args
, int * );
399 *va_arg( args
, int * ) = 1; /* Title offset */
400 *va_arg( args
, int * ) = 1; /* Chapter offset */
402 /* Duplicate title infos */
403 *pi_int
= p_sys
->i_titles
;
404 *ppp_title
= vlc_alloc( p_sys
->i_titles
, sizeof(input_title_t
*) );
405 for( i
= 0; i
< p_sys
->i_titles
; i
++ )
407 (*ppp_title
)[i
] = vlc_input_title_Duplicate(p_sys
->titles
[i
]);
411 case DEMUX_SET_TITLE
:
412 i
= va_arg( args
, int );
413 if( DvdReadSetArea( p_demux
, i
, 0, -1 ) != VLC_SUCCESS
)
415 msg_Warn( p_demux
, "cannot set title/chapter" );
418 p_demux
->info
.i_update
|=
419 INPUT_UPDATE_TITLE
| INPUT_UPDATE_SEEKPOINT
;
420 p_demux
->info
.i_title
= i
;
421 p_demux
->info
.i_seekpoint
= 0;
424 case DEMUX_SET_SEEKPOINT
:
425 i
= va_arg( args
, int );
426 if( DvdReadSetArea( p_demux
, -1, i
, -1 ) != VLC_SUCCESS
)
428 msg_Warn( p_demux
, "cannot set title/chapter" );
431 p_demux
->info
.i_update
|= INPUT_UPDATE_SEEKPOINT
;
432 p_demux
->info
.i_seekpoint
= i
;
435 case DEMUX_GET_PTS_DELAY
:
436 pi64
= va_arg( args
, int64_t * );
438 INT64_C(1000) * var_InheritInteger( p_demux
, "disc-caching" );
441 /* TODO implement others */
447 /*****************************************************************************
449 *****************************************************************************/
450 static int Demux( demux_t
*p_demux
)
452 demux_sys_t
*p_sys
= p_demux
->p_sys
;
454 uint8_t p_buffer
[DVD_VIDEO_LB_LEN
* DVD_BLOCK_READ_ONCE
];
455 int i_blocks_once
, i_read
;
458 * Playback by cell in this pgc, starting at the cell for our chapter.
462 * Check end of pack, and select the following one
464 if( !p_sys
->i_pack_len
)
466 /* Read NAV packet */
467 if( DVDReadBlocks( p_sys
->p_title
, p_sys
->i_next_vobu
,
470 msg_Err( p_demux
, "read failed for block %d", p_sys
->i_next_vobu
);
471 vlc_dialog_display_error( p_demux
, _("Playback failure"),
472 _("DVDRead could not read block %d."),
473 p_sys
->i_next_vobu
);
477 /* Basic check to be sure we don't have a empty title
478 * go to next title if so */
479 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
481 /* Parse the contained dsi packet */
482 DvdReadHandleDSI( p_demux
, p_buffer
);
485 if( p_sys
->i_cur_cell
>= p_sys
->p_cur_pgc
->nr_of_cells
)
487 int k
= p_sys
->i_title
;
489 /* Looking for a not broken title */
490 while( k
< p_sys
->i_titles
&& DvdReadSetArea( p_demux
, ++k
, 0, -1 ) != VLC_SUCCESS
)
492 msg_Err(p_demux
, "Failed next title, trying another: %i", k
);
493 if( k
>= p_sys
->i_titles
)
498 if( p_sys
->i_pack_len
>= 1024 )
500 msg_Err( p_demux
, "i_pack_len >= 1024 (%i). "
501 "This shouldn't happen!", p_sys
->i_pack_len
);
505 /* FIXME: Ugly kludge: we send the pack block to the input for it
506 * sometimes has a zero scr and restart the sync */
507 p_sys
->i_cur_block
++;
508 p_sys
->i_title_offset
++;
510 DemuxBlock( p_demux
, p_buffer
, DVD_VIDEO_LB_LEN
);
513 if( p_sys
->i_cur_cell
>= p_sys
->p_cur_pgc
->nr_of_cells
)
515 int k
= p_sys
->i_title
;
517 /* Looking for a not broken title */
518 while( k
< p_sys
->i_titles
&& DvdReadSetArea( p_demux
, ++k
, 0, -1 ) != VLC_SUCCESS
)
520 msg_Err(p_demux
, "Failed next title, trying another: %i", k
);
521 if( k
>= p_sys
->i_titles
)
529 i_blocks_once
= __MIN( p_sys
->i_pack_len
, DVD_BLOCK_READ_ONCE
);
530 p_sys
->i_pack_len
-= i_blocks_once
;
533 i_read
= DVDReadBlocks( p_sys
->p_title
, p_sys
->i_cur_block
,
534 i_blocks_once
, p_buffer
);
535 if( i_read
!= i_blocks_once
)
537 msg_Err( p_demux
, "read failed for %d/%d blocks at 0x%02x",
538 i_read
, i_blocks_once
, p_sys
->i_cur_block
);
539 vlc_dialog_display_error( p_demux
, _("Playback failure"),
540 _("DVDRead could not read %d/%d blocks at 0x%02x."),
541 i_read
, i_blocks_once
, p_sys
->i_cur_block
);
545 p_sys
->i_cur_block
+= i_read
;
546 p_sys
->i_title_offset
+= i_read
;
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
);
553 for( int i
= 0; i
< i_read
; i
++ )
555 DemuxBlock( p_demux
, p_buffer
+ i
* DVD_VIDEO_LB_LEN
,
564 /*****************************************************************************
565 * DemuxBlock: demux a given block
566 *****************************************************************************/
567 static int DemuxBlock( demux_t
*p_demux
, const uint8_t *p
, int len
)
569 demux_sys_t
*p_sys
= p_demux
->p_sys
;
573 int i_size
= ps_pkt_size( p
, len
);
574 if( i_size
<= 0 || i_size
> len
)
580 block_t
*p_pkt
= block_Alloc( i_size
);
581 memcpy( p_pkt
->p_buffer
, p
, i_size
);
583 /* Parse it and send it */
584 switch( 0x100 | p
[3] )
593 msg_Warn( p_demux
, "received a PSM packet" );
595 else if( p
[3] == 0xbb )
597 msg_Warn( p_demux
, "received a SYSTEM packet" );
600 block_Release( p_pkt
);
607 if( !ps_pkt_parse_pack( p_pkt
, &i_scr
, &i_mux_rate
) )
609 es_out_SetPCR( p_demux
->out
, i_scr
);
610 if( i_mux_rate
> 0 ) p_sys
->i_mux_rate
= i_mux_rate
;
612 block_Release( p_pkt
);
617 int i_id
= ps_pkt_id( p_pkt
);
620 ps_track_t
*tk
= &p_sys
->tk
[ps_id_to_tk(i_id
)];
622 if( !tk
->b_configured
)
624 ESNew( p_demux
, i_id
, 0 );
627 !ps_pkt_parse_pes( VLC_OBJECT(p_demux
), p_pkt
, tk
->i_skip
) )
629 es_out_Send( p_demux
->out
, tk
->es
, p_pkt
);
633 block_Release( p_pkt
);
638 block_Release( p_pkt
);
651 /*****************************************************************************
652 * ESNew: register a new elementary stream
653 *****************************************************************************/
654 static void ESNew( demux_t
*p_demux
, int i_id
, int i_lang
)
656 demux_sys_t
*p_sys
= p_demux
->p_sys
;
657 ps_track_t
*tk
= &p_sys
->tk
[ps_id_to_tk(i_id
)];
658 char psz_language
[3];
660 if( tk
->b_configured
) return;
662 if( ps_track_fill( tk
, 0, i_id
, NULL
, true ) )
664 msg_Warn( p_demux
, "unknown codec for id=0x%x", i_id
);
668 psz_language
[0] = psz_language
[1] = psz_language
[2] = 0;
669 if( i_lang
&& i_lang
!= 0xffff )
671 psz_language
[0] = (i_lang
>> 8)&0xff;
672 psz_language
[1] = (i_lang
)&0xff;
676 if( tk
->fmt
.i_cat
== VIDEO_ES
)
678 tk
->fmt
.video
.i_sar_num
= p_sys
->i_sar_num
;
679 tk
->fmt
.video
.i_sar_den
= p_sys
->i_sar_den
;
681 else if( tk
->fmt
.i_cat
== AUDIO_ES
)
685 /* find the audio number PLEASE find another way */
686 if( (i_id
&0xbdf8) == 0xbd88 ) /* dts */
690 else if( (i_id
&0xbdf0) == 0xbd80 ) /* a52 */
694 else if( (i_id
&0xbdf0) == 0xbda0 ) /* lpcm */
698 else if( ( i_id
&0xe0 ) == 0xc0 ) /* mpga */
704 if( psz_language
[0] ) tk
->fmt
.psz_language
= strdup( psz_language
);
706 else if( tk
->fmt
.i_cat
== SPU_ES
)
709 tk
->fmt
.subs
.spu
.palette
[0] = SPU_PALETTE_DEFINED
;
710 memcpy( &tk
->fmt
.subs
.spu
.palette
[1], p_sys
->clut
,
711 16 * sizeof( uint32_t ) );
713 if( psz_language
[0] ) tk
->fmt
.psz_language
= strdup( psz_language
);
716 tk
->es
= es_out_Add( p_demux
->out
, &tk
->fmt
);
717 tk
->b_configured
= true;
720 /*****************************************************************************
721 * DvdReadSetArea: initialize input data for title x, chapter y.
722 * It should be called for each user navigation request.
723 *****************************************************************************
724 * Take care that i_title and i_chapter start from 0.
725 *****************************************************************************/
726 static int DvdReadSetArea( demux_t
*p_demux
, int i_title
, int i_chapter
,
729 VLC_UNUSED( i_angle
);
731 demux_sys_t
*p_sys
= p_demux
->p_sys
;
732 int pgc_id
= 0, pgn
= 0;
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
738 if( i_title
>= 0 && i_title
< p_sys
->i_titles
&&
739 i_title
!= p_sys
->i_title
)
741 int i_start_cell
, i_end_cell
;
743 if( p_sys
->p_title
!= NULL
)
745 DVDCloseFile( p_sys
->p_title
);
746 p_sys
->p_title
= NULL
;
748 if( p_vts
!= NULL
) ifoClose( p_vts
);
749 p_sys
->i_title
= i_title
;
752 * We have to load all title information
754 msg_Dbg( p_demux
, "open VTS %d, for title %d",
755 p_vmg
->tt_srpt
->title
[i_title
].title_set_nr
, i_title
+ 1 );
758 if( !( p_vts
= ifoOpen( p_sys
->p_dvdread
,
759 p_vmg
->tt_srpt
->title
[i_title
].title_set_nr
) ) )
761 msg_Err( p_demux
, "fatal error in vts ifo" );
765 /* Title position inside the selected vts */
766 p_sys
->i_ttn
= p_vmg
->tt_srpt
->title
[i_title
].vts_ttn
;
768 /* Find title start/end */
769 pgc_id
= p_vts
->vts_ptt_srpt
->title
[p_sys
->i_ttn
- 1].ptt
[0].pgcn
;
770 pgn
= p_vts
->vts_ptt_srpt
->title
[p_sys
->i_ttn
- 1].ptt
[0].pgn
;
771 p_pgc
= p_vts
->vts_pgcit
->pgci_srp
[pgc_id
- 1].pgc
;
773 if( p_pgc
->cell_playback
== NULL
)
775 msg_Err( p_demux
, "Invalid PGC (cell_playback_offset)" );
779 p_sys
->i_title_start_cell
=
780 i_start_cell
= p_pgc
->program_map
[pgn
- 1] - 1;
781 p_sys
->i_title_start_block
=
782 p_pgc
->cell_playback
[i_start_cell
].first_sector
;
784 p_sys
->i_title_end_cell
=
785 i_end_cell
= p_pgc
->nr_of_cells
- 1;
786 p_sys
->i_title_end_block
=
787 p_pgc
->cell_playback
[i_end_cell
].last_sector
;
789 p_sys
->i_title_offset
= 0;
791 p_sys
->i_title_blocks
= 0;
792 for( int i
= i_start_cell
; i
<= i_end_cell
; i
++ )
794 p_sys
->i_title_blocks
+= p_pgc
->cell_playback
[i
].last_sector
-
795 p_pgc
->cell_playback
[i
].first_sector
+ 1;
798 msg_Dbg( p_demux
, "title %d vts_title %d pgc %d pgn %d "
799 "start %d end %d blocks: %d",
800 i_title
+ 1, p_sys
->i_ttn
, pgc_id
, pgn
,
801 p_sys
->i_title_start_block
, p_sys
->i_title_end_block
,
802 p_sys
->i_title_blocks
);
805 * Set properties for current chapter
807 p_sys
->i_chapter
= 0;
809 p_vts
->vts_ptt_srpt
->title
[p_sys
->i_ttn
- 1].nr_of_ptts
;
811 pgc_id
= p_vts
->vts_ptt_srpt
->title
[
812 p_sys
->i_ttn
- 1].ptt
[p_sys
->i_chapter
].pgcn
;
813 pgn
= p_vts
->vts_ptt_srpt
->title
[
814 p_sys
->i_ttn
- 1].ptt
[p_sys
->i_chapter
].pgn
;
816 p_pgc
= p_vts
->vts_pgcit
->pgci_srp
[pgc_id
- 1].pgc
;
817 p_sys
->i_pack_len
= 0;
819 p_sys
->i_cur_cell
= p_pgc
->program_map
[pgn
- 1] - 1;
820 DvdReadFindCell( p_demux
);
822 p_sys
->i_next_vobu
= p_sys
->i_cur_block
=
823 p_pgc
->cell_playback
[p_sys
->i_cur_cell
].first_sector
;
828 p_sys
->i_angles
= p_vmg
->tt_srpt
->title
[i_title
].nr_of_angles
;
829 if( p_sys
->i_angle
> p_sys
->i_angles
) p_sys
->i_angle
= 1;
832 * We've got enough info, time to open the title set data.
834 if( !( p_sys
->p_title
= DVDOpenFile( p_sys
->p_dvdread
,
835 p_vmg
->tt_srpt
->title
[i_title
].title_set_nr
,
836 DVD_READ_TITLE_VOBS
) ) )
838 msg_Err( p_demux
, "cannot open title (VTS_%02d_1.VOB)",
839 p_vmg
->tt_srpt
->title
[i_title
].title_set_nr
);
843 //IfoPrintTitle( p_demux );
846 * Destroy obsolete ES by reinitializing program 0
847 * and find all ES in title with ifo data
849 es_out_Control( p_demux
->out
, ES_OUT_RESET_PCR
);
851 for( int i
= 0; i
< PS_TK_COUNT
; i
++ )
853 ps_track_t
*tk
= &p_sys
->tk
[i
];
854 if( tk
->b_configured
)
856 es_format_Clean( &tk
->fmt
);
857 if( tk
->es
) es_out_Del( p_demux
->out
, tk
->es
);
859 tk
->b_configured
= false;
862 if( p_demux
->info
.i_title
!= i_title
)
864 p_demux
->info
.i_update
|=
865 INPUT_UPDATE_TITLE
| INPUT_UPDATE_SEEKPOINT
;
866 p_demux
->info
.i_title
= i_title
;
867 p_demux
->info
.i_seekpoint
= 0;
870 /* TODO: re-add angles */
873 ESNew( p_demux
, 0xe0, 0 ); /* Video, FIXME ? */
874 const video_attr_t
*p_attr
= &p_vts
->vtsi_mat
->vts_video_attr
;
875 int i_video_height
= p_attr
->video_format
!= 0 ? 576 : 480;
877 switch( p_attr
->picture_size
)
894 switch( p_attr
->display_aspect_ratio
)
897 p_sys
->i_sar_num
= 4 * i_video_height
;
898 p_sys
->i_sar_den
= 3 * i_video_width
;
901 p_sys
->i_sar_num
= 16 * i_video_height
;
902 p_sys
->i_sar_den
= 9 * i_video_width
;
905 p_sys
->i_sar_num
= 0;
906 p_sys
->i_sar_den
= 0;
910 #define audio_control \
911 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
913 /* Audio ES, in the order they appear in the .ifo */
914 for( int i
= 1; i
<= p_vts
->vtsi_mat
->nr_of_vts_audio_streams
; i
++ )
919 //IfoPrintAudio( p_demux, i );
921 /* Audio channel is active if first byte is 0x80 */
922 if( audio_control
& 0x8000 )
924 i_position
= ( audio_control
& 0x7F00 ) >> 8;
926 msg_Dbg( p_demux
, "audio position %d", i_position
);
927 switch( p_vts
->vtsi_mat
->vts_audio_attr
[i
- 1].audio_format
)
930 i_id
= (0x80 + i_position
) | 0xbd00;
933 case 0x03: /* MPEG audio */
934 i_id
= 0xc000 + i_position
;
936 case 0x04: /* LPCM */
937 i_id
= (0xa0 + i_position
) | 0xbd00;
940 i_id
= (0x88 + i_position
) | 0xbd00;
944 msg_Err( p_demux
, "unknown audio type %.2x",
945 p_vts
->vtsi_mat
->vts_audio_attr
[i
- 1].audio_format
);
948 ESNew( p_demux
, i_id
, p_sys
->p_vts_file
->vtsi_mat
->
949 vts_audio_attr
[i
- 1].lang_code
);
954 #define spu_palette \
955 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette
957 memcpy( p_sys
->clut
, spu_palette
, 16 * sizeof( uint32_t ) );
959 #define spu_control \
960 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
963 for( int i
= 1; i
<= p_vts
->vtsi_mat
->nr_of_vts_subp_streams
; i
++ )
968 //IfoPrintSpu( p_sys, i );
969 msg_Dbg( p_demux
, "spu %d 0x%02x", i
, spu_control
);
971 if( spu_control
& 0x80000000 )
973 /* there are several streams for one spu */
974 if( p_vts
->vtsi_mat
->vts_video_attr
.display_aspect_ratio
)
977 switch( p_vts
->vtsi_mat
->vts_video_attr
.permitted_df
)
979 case 1: /* letterbox */
980 i_position
= spu_control
& 0xff;
982 case 2: /* pan&scan */
983 i_position
= ( spu_control
>> 8 ) & 0xff;
985 default: /* widescreen */
986 i_position
= ( spu_control
>> 16 ) & 0xff;
993 i_position
= ( spu_control
>> 24 ) & 0x7F;
996 i_id
= (0x20 + i_position
) | 0xbd00;
998 ESNew( p_demux
, i_id
, p_sys
->p_vts_file
->vtsi_mat
->
999 vts_subp_attr
[i
- 1].lang_code
);
1005 else if( i_title
!= -1 && i_title
!= p_sys
->i_title
)
1008 return VLC_EGENERIC
; /* Couldn't set title */
1015 if( i_chapter
>= 0 && i_chapter
< p_sys
->i_chapters
)
1017 pgc_id
= p_vts
->vts_ptt_srpt
->title
[
1018 p_sys
->i_ttn
- 1].ptt
[i_chapter
].pgcn
;
1019 pgn
= p_vts
->vts_ptt_srpt
->title
[
1020 p_sys
->i_ttn
- 1].ptt
[i_chapter
].pgn
;
1022 p_pgc
= p_vts
->vts_pgcit
->pgci_srp
[pgc_id
- 1].pgc
;
1023 if( p_pgc
->cell_playback
== NULL
)
1024 return VLC_EGENERIC
; /* Couldn't set chapter */
1026 p_sys
->i_cur_cell
= p_pgc
->program_map
[pgn
- 1] - 1;
1027 p_sys
->i_chapter
= i_chapter
;
1028 DvdReadFindCell( p_demux
);
1030 p_sys
->i_title_offset
= 0;
1031 for( int i
= p_sys
->i_title_start_cell
; i
< p_sys
->i_cur_cell
; i
++ )
1033 p_sys
->i_title_offset
+= p_pgc
->cell_playback
[i
].last_sector
-
1034 p_pgc
->cell_playback
[i
].first_sector
+ 1;
1037 p_sys
->i_pack_len
= 0;
1038 p_sys
->i_next_vobu
= p_sys
->i_cur_block
=
1039 p_pgc
->cell_playback
[p_sys
->i_cur_cell
].first_sector
;
1041 if( p_demux
->info
.i_seekpoint
!= i_chapter
)
1043 p_demux
->info
.i_update
|= INPUT_UPDATE_SEEKPOINT
;
1044 p_demux
->info
.i_seekpoint
= i_chapter
;
1047 else if( i_chapter
!= -1 )
1050 return VLC_EGENERIC
; /* Couldn't set chapter */
1060 /*****************************************************************************
1061 * DvdReadSeek : Goes to a given position on the stream.
1062 *****************************************************************************
1063 * This one is used by the input and translate chronological position from
1064 * input to logical position on the device.
1065 *****************************************************************************/
1066 static void DvdReadSeek( demux_t
*p_demux
, int i_block_offset
)
1068 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1075 #define p_pgc p_sys->p_cur_pgc
1076 #define p_vts p_sys->p_vts_file
1079 i_block
= i_block_offset
;
1080 for( i_cell
= p_sys
->i_title_start_cell
;
1081 i_cell
<= p_sys
->i_title_end_cell
; i_cell
++ )
1083 if( i_block
< (int)p_pgc
->cell_playback
[i_cell
].last_sector
-
1084 (int)p_pgc
->cell_playback
[i_cell
].first_sector
+ 1 ) break;
1086 i_block
-= (p_pgc
->cell_playback
[i_cell
].last_sector
-
1087 p_pgc
->cell_playback
[i_cell
].first_sector
+ 1);
1089 if( i_cell
> p_sys
->i_title_end_cell
)
1091 msg_Err( p_demux
, "couldn't find cell for block %i", i_block_offset
);
1094 i_block
+= p_pgc
->cell_playback
[i_cell
].first_sector
;
1095 p_sys
->i_title_offset
= i_block_offset
;
1098 for( i_chapter
= 0; i_chapter
< p_sys
->i_chapters
; i_chapter
++ )
1100 int pgc_id
, pgn
, i_tmp
;
1102 pgc_id
= p_vts
->vts_ptt_srpt
->title
[
1103 p_sys
->i_ttn
- 1].ptt
[i_chapter
].pgcn
;
1104 pgn
= p_vts
->vts_ptt_srpt
->title
[
1105 p_sys
->i_ttn
- 1].ptt
[i_chapter
].pgn
;
1107 i_tmp
= p_vts
->vts_pgcit
->pgci_srp
[pgc_id
- 1].pgc
->program_map
[pgn
-1];
1109 if( i_tmp
> i_cell
) break;
1112 if( i_chapter
< p_sys
->i_chapters
&&
1113 p_demux
->info
.i_seekpoint
!= i_chapter
)
1115 p_demux
->info
.i_update
|= INPUT_UPDATE_SEEKPOINT
;
1116 p_demux
->info
.i_seekpoint
= i_chapter
;
1120 while( (int)p_vts
->vts_vobu_admap
->vobu_start_sectors
[i_vobu
] <= i_block
)
1126 while( p_vts
->vts_c_adt
->cell_adr_table
[i_sub_cell
].start_sector
<
1127 p_vts
->vts_vobu_admap
->vobu_start_sectors
[i_vobu
-1] )
1133 msg_Dbg( p_demux
, "cell %d i_sub_cell %d chapter %d vobu %d "
1134 "cell_sector %d vobu_sector %d sub_cell_sector %d",
1135 i_cell
, i_sub_cell
, i_chapter
, i_vobu
,
1136 p_sys
->p_cur_pgc
->cell_playback
[i_cell
].first_sector
,
1137 p_vts
->vts_vobu_admap
->vobu_start_sectors
[i_vobu
],
1138 p_vts
->vts_c_adt
->cell_adr_table
[i_sub_cell
- 1].start_sector
);
1141 p_sys
->i_cur_block
= i_block
;
1142 p_sys
->i_next_vobu
= p_vts
->vts_vobu_admap
->vobu_start_sectors
[i_vobu
];
1143 p_sys
->i_pack_len
= p_sys
->i_next_vobu
- i_block
;
1144 p_sys
->i_cur_cell
= i_cell
;
1145 p_sys
->i_chapter
= i_chapter
;
1146 DvdReadFindCell( p_demux
);
1154 /*****************************************************************************
1156 *****************************************************************************/
1157 static void DvdReadHandleDSI( demux_t
*p_demux
, uint8_t *p_data
)
1159 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1161 navRead_DSI( &p_sys
->dsi_pack
, &p_data
[DSI_START_BYTE
] );
1164 * Determine where we go next. These values are the ones we mostly
1167 p_sys
->i_cur_block
= p_sys
->dsi_pack
.dsi_gi
.nv_pck_lbn
;
1168 p_sys
->i_pack_len
= p_sys
->dsi_pack
.dsi_gi
.vobu_ea
;
1171 * Store the timecodes so we can get the current time
1173 p_sys
->i_title_cur_time
= (mtime_t
) p_sys
->dsi_pack
.dsi_gi
.nv_pck_scr
/ 90 * 1000;
1174 p_sys
->i_cell_cur_time
= (mtime_t
) dvdtime_to_time( &p_sys
->dsi_pack
.dsi_gi
.c_eltm
, 0 );
1177 * If we're not at the end of this cell, we can determine the next
1178 * VOBU to display using the VOBU_SRI information section of the
1179 * DSI. Using this value correctly follows the current angle,
1180 * avoiding the doubled scenes in The Matrix, and makes our life
1184 p_sys
->i_next_vobu
= p_sys
->i_cur_block
+
1185 ( p_sys
->dsi_pack
.vobu_sri
.next_vobu
& 0x7fffffff );
1187 if( p_sys
->dsi_pack
.vobu_sri
.next_vobu
!= SRI_END_OF_CELL
1188 && p_sys
->i_angle
> 1 )
1190 switch( ( p_sys
->dsi_pack
.sml_pbi
.category
& 0xf000 ) >> 12 )
1193 /* Interleaved unit with no angle */
1194 if( p_sys
->dsi_pack
.sml_pbi
.ilvu_sa
!= 0 )
1196 p_sys
->i_next_vobu
= p_sys
->i_cur_block
+
1197 p_sys
->dsi_pack
.sml_pbi
.ilvu_sa
;
1198 p_sys
->i_pack_len
= p_sys
->dsi_pack
.sml_pbi
.ilvu_ea
;
1202 p_sys
->i_next_vobu
= p_sys
->i_cur_block
+
1203 p_sys
->dsi_pack
.dsi_gi
.vobu_ea
+ 1;
1207 /* vobu is end of ilvu */
1208 if( p_sys
->dsi_pack
.sml_agli
.data
[p_sys
->i_angle
-1].address
)
1210 p_sys
->i_next_vobu
= p_sys
->i_cur_block
+
1211 p_sys
->dsi_pack
.sml_agli
.data
[p_sys
->i_angle
-1].address
;
1212 p_sys
->i_pack_len
= p_sys
->dsi_pack
.sml_pbi
.ilvu_ea
;
1217 /* vobu is beginning of ilvu */
1221 /* entering interleaved section */
1223 /* non interleaved cells in interleaved section */
1225 p_sys
->i_next_vobu
= p_sys
->i_cur_block
+
1226 ( p_sys
->dsi_pack
.vobu_sri
.next_vobu
& 0x7fffffff );
1230 else if( p_sys
->dsi_pack
.vobu_sri
.next_vobu
== SRI_END_OF_CELL
)
1232 p_sys
->i_cur_cell
= p_sys
->i_next_cell
;
1235 if( p_sys
->i_cur_cell
>= p_sys
->p_cur_pgc
->nr_of_cells
) return;
1237 DvdReadFindCell( p_demux
);
1239 p_sys
->i_next_vobu
=
1240 p_sys
->p_cur_pgc
->cell_playback
[p_sys
->i_cur_cell
].first_sector
;
1242 p_sys
->i_cell_duration
= (mtime_t
)dvdtime_to_time( &p_sys
->p_cur_pgc
->cell_playback
[p_sys
->i_cur_cell
].playback_time
, 0 );
1247 msg_Dbg( p_demux
, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d c_time %lld",
1248 p_sys
->dsi_pack
.dsi_gi
.nv_pck_scr
,
1249 p_sys
->dsi_pack
.dsi_gi
.nv_pck_lbn
,
1250 p_sys
->dsi_pack
.dsi_gi
.vobu_ea
,
1251 p_sys
->dsi_pack
.dsi_gi
.vobu_vob_idn
,
1252 p_sys
->dsi_pack
.dsi_gi
.vobu_c_idn
,
1253 dvdtime_to_time( &p_sys
->dsi_pack
.dsi_gi
.c_eltm
, 0 ) );
1255 msg_Dbg( p_demux
, "cell duration: %lld",
1256 (mtime_t
)dvdtime_to_time( &p_sys
->p_cur_pgc
->cell_playback
[p_sys
->i_cur_cell
].playback_time
, 0 ) );
1258 msg_Dbg( p_demux
, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1259 p_sys
->dsi_pack
.sml_pbi
.category
,
1260 p_sys
->dsi_pack
.sml_pbi
.ilvu_ea
,
1261 p_sys
->dsi_pack
.sml_pbi
.ilvu_sa
,
1262 p_sys
->dsi_pack
.sml_pbi
.size
);
1264 msg_Dbg( p_demux
, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1265 p_sys
->dsi_pack
.vobu_sri
.next_vobu
& 0x7fffffff,
1266 p_sys
->dsi_pack
.sml_agli
.data
[ p_sys
->i_angle
- 1 ].address
,
1267 p_sys
->dsi_pack
.sml_agli
.data
[ p_sys
->i_angle
].address
);
1271 /*****************************************************************************
1273 *****************************************************************************/
1274 static void DvdReadFindCell( demux_t
*p_demux
)
1276 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1282 #define cell p_sys->p_cur_pgc->cell_playback
1284 if( cell
[p_sys
->i_cur_cell
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
)
1286 p_sys
->i_cur_cell
+= p_sys
->i_angle
- 1;
1288 while( cell
[p_sys
->i_cur_cell
+i
].block_mode
!= BLOCK_MODE_LAST_CELL
)
1292 p_sys
->i_next_cell
= p_sys
->i_cur_cell
+ i
+ 1;
1296 p_sys
->i_next_cell
= p_sys
->i_cur_cell
+ 1;
1301 if( p_sys
->i_chapter
+ 1 >= p_sys
->i_chapters
) return;
1303 pgc_id
= p_sys
->p_vts_file
->vts_ptt_srpt
->title
[
1304 p_sys
->i_ttn
- 1].ptt
[p_sys
->i_chapter
+ 1].pgcn
;
1305 pgn
= p_sys
->p_vts_file
->vts_ptt_srpt
->title
[
1306 p_sys
->i_ttn
- 1].ptt
[p_sys
->i_chapter
+ 1].pgn
;
1307 p_pgc
= p_sys
->p_vts_file
->vts_pgcit
->pgci_srp
[pgc_id
- 1].pgc
;
1309 if( p_sys
->i_cur_cell
>= p_pgc
->program_map
[pgn
- 1] - 1 )
1313 if( p_sys
->i_chapter
< p_sys
->i_chapters
&&
1314 p_demux
->info
.i_seekpoint
!= p_sys
->i_chapter
)
1316 p_demux
->info
.i_update
|= INPUT_UPDATE_SEEKPOINT
;
1317 p_demux
->info
.i_seekpoint
= p_sys
->i_chapter
;
1322 /*****************************************************************************
1323 * DemuxTitles: get the titles/chapters structure
1324 *****************************************************************************/
1325 static void DemuxTitles( demux_t
*p_demux
, int *pi_angle
)
1327 VLC_UNUSED( pi_angle
);
1329 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1333 /* Find out number of titles/chapters */
1334 #define tt_srpt p_sys->p_vmg_file->tt_srpt
1336 int32_t i_titles
= tt_srpt
->nr_of_srpts
;
1337 msg_Dbg( p_demux
, "number of titles: %d", i_titles
);
1339 for( int i
= 0; i
< i_titles
; i
++ )
1341 int32_t i_chapters
= 0;
1344 i_chapters
= tt_srpt
->title
[i
].nr_of_ptts
;
1345 msg_Dbg( p_demux
, "title %d has %d chapters", i
, i_chapters
);
1347 t
= vlc_input_title_New();
1349 for( j
= 0; j
< __MAX( i_chapters
, 1 ); j
++ )
1351 s
= vlc_seekpoint_New();
1352 TAB_APPEND( t
->i_seekpoint
, t
->seekpoint
, s
);
1355 TAB_APPEND( p_sys
->i_titles
, p_sys
->titles
, t
);