1 /*****************************************************************************
2 * dvdread.c : DvdRead input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004 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
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
30 #include <string.h> /* strdup() */
33 #include <vlc/input.h>
37 #include "../demux/ps.h"
44 #include <sys/types.h>
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>
54 /*****************************************************************************
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." )
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 " \
77 "The default method is: key.")
79 static char *psz_css_list[] = { "title", "disc", "key" };
80 static char *psz_css_list_text[] = { N_("title"), N_("Disc"), N_("Key") };
82 static int Open ( vlc_object_t * );
83 static void Close( vlc_object_t * );
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 );
99 /* how many blocks DVDRead will read in each loop */
100 #define DVD_BLOCK_READ_ONCE 4
102 /*****************************************************************************
104 *****************************************************************************/
109 dvd_reader_t *p_dvdread;
112 ifo_handle_t *p_vmg_file;
113 ifo_handle_t *p_vts_file;
115 unsigned int i_title;
116 unsigned int i_chapter, i_chapters;
117 unsigned int i_angle, i_angles;
119 tt_srpt_t *p_tt_srpt;
126 unsigned int i_pack_len;
127 unsigned int i_cur_block;
128 unsigned int i_next_vobu;
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;
136 unsigned int i_title_start_cell;
137 unsigned int i_cur_cell;
138 unsigned int i_next_cell;
141 ps_track_t tk[PS_TK_COUNT];
143 /* for spu variables */
144 input_thread_t *p_input;
154 input_title_t **titles;
157 static char *ParseCL( vlc_object_t *, char *, vlc_bool_t, int *, int *, int *);
159 static int Control ( demux_t *, int, va_list );
160 static int Demux ( demux_t * );
161 static int DemuxBlock( demux_t *, uint8_t *, int );
165 AR_SQUARE_PICTURE = 1, /* square pixels */
166 AR_3_4_PICTURE = 2, /* 3:4 picture (TV) */
167 AR_16_9_PICTURE = 3, /* 16:9 picture (wide screen) */
168 AR_221_1_PICTURE = 4, /* 2.21:1 picture (movie) */
171 static void DemuxTitles( demux_t *, int *, int *, int * );
172 static void ESNew( demux_t *, int, int );
174 static int DvdReadSetArea ( demux_t *, int, int, int );
175 static void DvdReadSeek ( demux_t *, int );
176 static void DvdReadHandleDSI( demux_t *, uint8_t * );
177 static void DvdReadFindCell ( demux_t * );
179 /*****************************************************************************
181 *****************************************************************************/
182 static int Open( vlc_object_t *p_this )
184 demux_t *p_demux = (demux_t*)p_this;
186 int i_title, i_chapter, i_angle;
188 char *psz_dvdcss_env;
189 dvd_reader_t *p_dvdread;
190 ifo_handle_t *p_vmg_file;
192 psz_name = ParseCL( VLC_OBJECT(p_demux), p_demux->psz_path, VLC_TRUE,
193 &i_title, &i_chapter, &i_angle );
199 /* Override environment variable DVDCSS_METHOD with config option
200 * (FIXME: this creates a small memory leak) */
201 psz_dvdcss_env = config_GetPsz( p_demux, "dvdread-css-method" );
202 if( psz_dvdcss_env && *psz_dvdcss_env )
206 psz_env = malloc( strlen("DVDCSS_METHOD=") +
207 strlen( psz_dvdcss_env ) + 1 );
211 free( psz_dvdcss_env );
215 sprintf( psz_env, "%s%s", "DVDCSS_METHOD=", psz_dvdcss_env );
219 if( psz_dvdcss_env ) free( psz_dvdcss_env );
222 if( !(p_dvdread = DVDOpen( psz_name )) )
224 msg_Err( p_demux, "DVDRead cannot open source: %s", psz_name );
230 /* Ifo allocation & initialisation */
231 if( !( p_vmg_file = ifoOpen( p_dvdread, 0 ) ) )
233 msg_Warn( p_demux, "cannot open VMG info" );
236 msg_Dbg( p_demux, "VMG opened" );
238 /* Fill p_demux field */
239 p_demux->pf_demux = Demux;
240 p_demux->pf_control = Control;
241 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
242 memset( p_sys, 0, sizeof( demux_sys_t ) );
244 ps_track_init( p_sys->tk );
245 p_sys->i_aspect = -1;
247 p_sys->p_dvdread = p_dvdread;
248 p_sys->p_vmg_file = p_vmg_file;
249 p_sys->p_title = NULL;
250 p_sys->p_vts_file = NULL;
252 p_sys->i_title = p_sys->i_chapter = -1;
253 p_sys->i_angle = i_angle;
255 DemuxTitles( p_demux, &i_title, &i_chapter, &i_angle );
257 DvdReadSetArea( p_demux, i_title - 1, i_chapter, i_angle );
259 /* Update default_pts to a suitable value for dvdread access */
260 var_Create( p_demux, "dvdread-caching",
261 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
266 /*****************************************************************************
268 *****************************************************************************/
269 static void Close( vlc_object_t *p_this )
271 demux_t *p_demux = (demux_t*)p_this;
272 demux_sys_t *p_sys = p_demux->p_sys;
275 for( i = 0; i < PS_TK_COUNT; i++ )
277 ps_track_t *tk = &p_sys->tk[i];
280 es_format_Clean( &tk->fmt );
281 if( tk->es ) es_out_Del( p_demux->out, tk->es );
285 /* Close libdvdread */
286 if( p_sys->p_title ) DVDCloseFile( p_sys->p_title );
287 if( p_sys->p_vts_file ) ifoClose( p_sys->p_vts_file );
288 if( p_sys->p_vmg_file ) ifoClose( p_sys->p_vmg_file );
289 DVDClose( p_sys->p_dvdread );
294 /*****************************************************************************
296 *****************************************************************************/
297 static int Control( demux_t *p_demux, int i_query, va_list args )
299 demux_sys_t *p_sys = p_demux->p_sys;
303 input_title_t ***ppp_title;
309 case DEMUX_GET_POSITION:
311 pf = (double*) va_arg( args, double* );
313 if( p_sys->i_title_blocks > 0 )
314 *pf = (double)p_sys->i_title_offset / p_sys->i_title_blocks;
320 case DEMUX_SET_POSITION:
322 f = (double)va_arg( args, double );
324 DvdReadSeek( p_demux, f * p_sys->i_title_blocks );
329 /* Special for access_demux */
330 case DEMUX_CAN_PAUSE:
331 case DEMUX_CAN_CONTROL_PACE:
333 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
337 case DEMUX_SET_PAUSE_STATE:
340 case DEMUX_GET_TITLE_INFO:
341 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
342 pi_int = (int*)va_arg( args, int* );
344 /* Duplicate title infos */
345 *pi_int = p_sys->i_titles;
346 *ppp_title = malloc( sizeof(input_title_t **) * p_sys->i_titles );
347 for( i = 0; i < p_sys->i_titles; i++ )
349 (*ppp_title)[i] = vlc_input_title_Duplicate(p_sys->titles[i]);
353 case DEMUX_SET_TITLE:
354 i = (int)va_arg( args, int );
355 if( DvdReadSetArea( p_demux, i, 0, -1 ) != VLC_SUCCESS )
357 msg_Warn( p_demux, "cannot set title/chapter" );
360 p_demux->info.i_update |=
361 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
362 p_demux->info.i_title = i;
363 p_demux->info.i_seekpoint = 0;
366 case DEMUX_SET_SEEKPOINT:
367 i = (int)va_arg( args, int );
368 if( DvdReadSetArea( p_demux, -1, i, -1 ) != VLC_SUCCESS )
370 msg_Warn( p_demux, "cannot set title/chapter" );
373 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
374 p_demux->info.i_seekpoint = i;
377 case DEMUX_GET_PTS_DELAY:
378 pi64 = (int64_t*)va_arg( args, int64_t * );
379 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdread-caching" )*1000;
382 /* TODO implement others */
388 /*****************************************************************************
390 *****************************************************************************/
391 static int Demux( demux_t *p_demux )
393 demux_sys_t *p_sys = p_demux->p_sys;
395 uint8_t p_buffer[DVD_VIDEO_LB_LEN * DVD_BLOCK_READ_ONCE];
396 int i_blocks_once, i_read;
400 * Playback by cell in this pgc, starting at the cell for our chapter.
404 * Check end of pack, and select the following one
406 if( !p_sys->i_pack_len )
408 /* Read NAV packet */
409 if( DVDReadBlocks( p_sys->p_title, p_sys->i_next_vobu,
412 msg_Err( p_demux, "read failed for block %d", p_sys->i_next_vobu );
416 /* Basic check to be sure we don't have a empty title
417 * go to next title if so */
418 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
420 /* Parse the contained dsi packet */
421 DvdReadHandleDSI( p_demux, p_buffer );
424 if( p_sys->i_next_vobu > p_sys->i_title_end_block )
426 if( p_sys->i_title + 1 >= p_sys->i_titles ) return 0; /* EOF */
428 DvdReadSetArea( p_demux, p_sys->i_title + 1, 0, -1 );
431 if( p_sys->i_pack_len <= 0 )
433 msg_Err( p_demux, "i_pack_len <= 0. This shouldn't happen!" );
437 /* FIXME: Ugly kludge: we send the pack block to the input for it
438 * sometimes has a zero scr and restart the sync */
439 p_sys->i_cur_block++;
440 p_sys->i_title_offset++;
442 DemuxBlock( p_demux, p_buffer, DVD_VIDEO_LB_LEN );
445 if( p_sys->i_cur_block > p_sys->i_title_end_block )
447 if( p_sys->i_title + 1 >= p_sys->i_titles ) return 0; /* EOF */
449 DvdReadSetArea( p_demux, p_sys->i_title + 1, 0, -1 );
455 i_blocks_once = __MIN( p_sys->i_pack_len, DVD_BLOCK_READ_ONCE );
456 p_sys->i_pack_len -= i_blocks_once;
459 i_read = DVDReadBlocks( p_sys->p_title, p_sys->i_cur_block,
460 i_blocks_once, p_buffer );
461 if( i_read != i_blocks_once )
463 msg_Err( p_demux, "read failed for %d/%d blocks at 0x%02x",
464 i_read, i_blocks_once, p_sys->i_cur_block );
468 p_sys->i_cur_block += i_read;
469 p_sys->i_title_offset += i_read;
472 msg_Dbg( p_demux, "i_blocks: %d len: %d current: 0x%02x",
473 i_read, p_sys->i_pack_len, p_sys->i_cur_block );
476 for( i = 0; i < i_read; i++ )
478 DemuxBlock( p_demux, p_buffer + i * DVD_VIDEO_LB_LEN,
487 /*****************************************************************************
488 * DemuxBlock: demux a given block
489 *****************************************************************************/
490 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
492 demux_sys_t *p_sys = p_demux->p_sys;
495 while( p < &pkt[i_pkt] )
497 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
505 p_pkt = block_New( p_demux, i_size );
506 memcpy( p_pkt->p_buffer, p, i_size);
508 /* Parse it and send it */
509 switch( 0x100 | p[3] )
518 msg_Warn( p_demux, "received a PSM packet" );
520 else if( p[3] == 0xbb )
522 msg_Warn( p_demux, "received a SYSTEM packet" );
525 block_Release( p_pkt );
532 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
534 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
536 block_Release( p_pkt );
541 int i_id = ps_pkt_id( p_pkt );
544 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
548 ESNew( p_demux, i_id, 0 );
550 if( tk->b_seen && tk->es &&
551 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
553 es_out_Send( p_demux->out, tk->es, p_pkt );
557 block_Release( p_pkt );
562 block_Release( p_pkt );
574 /*****************************************************************************
575 * ESNew: register a new elementary stream
576 *****************************************************************************/
577 static void ESNew( demux_t *p_demux, int i_id, int i_lang )
579 demux_sys_t *p_sys = p_demux->p_sys;
580 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
581 char psz_language[3];
583 if( tk->b_seen ) return;
585 if( ps_track_fill( tk, i_id ) )
587 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
591 psz_language[0] = psz_language[1] = psz_language[2] = 0;
592 if( i_lang && i_lang != 0xffff )
594 psz_language[0] = (i_lang >> 8)&0xff;
595 psz_language[1] = (i_lang )&0xff;
599 if( tk->fmt.i_cat == VIDEO_ES )
601 if( p_sys->i_aspect >= 0 )
603 tk->fmt.video.i_aspect = p_sys->i_aspect;
606 else if( tk->fmt.i_cat == AUDIO_ES )
609 /* find the audio number PLEASE find another way */
610 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
614 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
618 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
622 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
627 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
629 else if( tk->fmt.i_cat == SPU_ES )
632 tk->fmt.subs.spu.palette[0] = 0xBeef;
633 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
634 16 * sizeof( uint32_t ) );
636 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
639 tk->es = es_out_Add( p_demux->out, &tk->fmt );
640 tk->b_seen = VLC_TRUE;
643 /*****************************************************************************
644 * DvdReadSetArea: initialize input data for title x, chapter y.
645 * It should be called for each user navigation request.
646 *****************************************************************************
647 * Take care that i_title and i_chapter start from 0.
648 *****************************************************************************/
649 static int DvdReadSetArea( demux_t *p_demux, int i_title, int i_chapter,
652 demux_sys_t *p_sys = p_demux->p_sys;
658 #define p_pgc p_sys->p_cur_pgc
659 #define p_vmg p_sys->p_vmg_file
660 #define p_vts p_sys->p_vts_file
662 if( i_title >= 0 && i_title < p_sys->i_titles &&
663 i_title != p_sys->i_title )
665 int i_start_cell, i_end_cell;
667 if( p_sys->p_title != NULL ) DVDCloseFile( p_sys->p_title );
668 if( p_vts != NULL ) ifoClose( p_vts );
669 p_sys->i_title = i_title;
672 * We have to load all title information
674 msg_Dbg( p_demux, "open VTS %d, for title %d",
675 p_vmg->tt_srpt->title[i_title].title_set_nr, i_title + 1 );
678 if( !( p_vts = ifoOpen( p_sys->p_dvdread,
679 p_vmg->tt_srpt->title[i_title].title_set_nr ) ) )
681 msg_Err( p_demux, "fatal error in vts ifo" );
685 /* Title position inside the selected vts */
686 p_sys->i_ttn = p_vmg->tt_srpt->title[i_title].vts_ttn;
688 /* Find title start/end */
689 pgc_id = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgcn;
690 pgn = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgn;
691 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
693 p_sys->i_title_start_cell =
694 i_start_cell = p_pgc->program_map[pgn - 1] - 1;
695 p_sys->i_title_start_block =
696 p_pgc->cell_playback[i_start_cell].first_sector;
698 i_end_cell = p_pgc->nr_of_cells - 1;
699 p_sys->i_title_end_block =
700 p_pgc->cell_playback[i_end_cell].last_sector;
702 p_sys->i_title_offset = 0;
704 p_sys->i_title_blocks = 0;
705 for( i = i_start_cell; i <= i_end_cell; i++ )
707 p_sys->i_title_blocks += p_pgc->cell_playback[i].last_sector -
708 p_pgc->cell_playback[i].first_sector + 1;
711 msg_Dbg( p_demux, "title %d vts_title %d pgc %d pgn %d "
712 "start %d end %d blocks: %d",
713 i_title + 1, p_sys->i_ttn, pgc_id, pgn,
714 p_sys->i_title_start_block, p_sys->i_title_end_block,
715 p_sys->i_title_blocks );
718 * Set properties for current chapter
720 p_sys->i_chapter = 0;
722 p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].nr_of_ptts;
724 pgc_id = p_vts->vts_ptt_srpt->title[
725 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgcn;
726 pgn = p_vts->vts_ptt_srpt->title[
727 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgn;
729 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
730 p_sys->i_pack_len = 0;
732 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
733 DvdReadFindCell( p_demux );
735 p_sys->i_next_vobu = p_sys->i_cur_block =
736 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
741 p_sys->i_angles = p_vmg->tt_srpt->title[i_title].nr_of_angles;
742 if( p_sys->i_angle > p_sys->i_angles ) p_sys->i_angle = 1;
745 * We've got enough info, time to open the title set data.
747 if( !( p_sys->p_title = DVDOpenFile( p_sys->p_dvdread,
748 p_vmg->tt_srpt->title[i_title].title_set_nr,
749 DVD_READ_TITLE_VOBS ) ) )
751 msg_Err( p_demux, "cannot open title (VTS_%02d_1.VOB)",
752 p_vmg->tt_srpt->title[i_title].title_set_nr );
756 //IfoPrintTitle( p_demux );
759 * Destroy obsolete ES by reinitializing program 0
760 * and find all ES in title with ifo data
762 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
764 for( i = 0; i < PS_TK_COUNT; i++ )
766 ps_track_t *tk = &p_sys->tk[i];
769 es_format_Clean( &tk->fmt );
770 if( tk->es ) es_out_Del( p_demux->out, tk->es );
772 tk->b_seen = VLC_FALSE;
775 if( p_demux->info.i_title != i_title )
777 p_demux->info.i_update |=
778 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
779 p_demux->info.i_title = i_title;
780 p_demux->info.i_seekpoint = 0;
783 /* TODO: re-add angles */
786 ESNew( p_demux, 0xe0, 0 ); /* Video, FIXME ? */
788 #define audio_control \
789 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
791 /* Audio ES, in the order they appear in the .ifo */
792 for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
797 //IfoPrintAudio( p_demux, i );
799 /* Audio channel is active if first byte is 0x80 */
800 if( audio_control & 0x8000 )
802 i_position = ( audio_control & 0x7F00 ) >> 8;
804 msg_Dbg( p_demux, "audio position %d", i_position );
805 switch( p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format )
808 i_id = (0x80 + i_position) | 0xbd00;
811 case 0x03: /* MPEG audio */
812 i_id = 0xc000 + i_position;
814 case 0x04: /* LPCM */
815 i_id = (0xa0 + i_position) | 0xbd00;
818 i_id = (0x88 + i_position) | 0xbd00;
822 msg_Err( p_demux, "unknown audio type %.2x",
823 p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format );
826 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
827 vts_audio_attr[i - 1].lang_code );
832 #define spu_control \
833 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
836 for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
841 //IfoPrintSpu( p_sys, i );
842 msg_Dbg( p_demux, "spu %d 0x%02x", i, spu_control );
844 if( spu_control & 0x80000000 )
846 /* there are several streams for one spu */
847 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
850 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
853 i_position = spu_control & 0xff;
856 i_position = ( spu_control >> 8 ) & 0xff;
859 i_position = ( spu_control >> 16 ) & 0xff;
866 i_position = ( spu_control >> 24 ) & 0x7F;
869 i_id = (0x20 + i_position) | 0xbd00;
871 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
872 vts_subp_attr[i - 1].lang_code );
880 /* TODO: update input ? */
887 if( i_chapter >= 0 && i_chapter <= p_sys->i_chapters &&
888 i_chapter != p_sys->i_chapter )
890 pgc_id = p_vts->vts_ptt_srpt->title[
891 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
892 pgn = p_vts->vts_ptt_srpt->title[
893 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
895 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
897 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
898 p_sys->i_chapter = i_chapter;
899 DvdReadFindCell( p_demux );
901 p_sys->i_title_offset = 0;
902 for( i = p_sys->i_title_start_cell; i < p_sys->i_cur_cell; i++ )
904 p_sys->i_title_offset += p_pgc->cell_playback[i].last_sector -
905 p_pgc->cell_playback[i].first_sector + 1;
908 p_sys->i_pack_len = 0;
909 p_sys->i_next_vobu = p_sys->i_cur_block =
910 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
912 if( p_demux->info.i_seekpoint != i_chapter )
914 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
915 p_demux->info.i_seekpoint = i_chapter;
926 /*****************************************************************************
927 * DvdReadSeek : Goes to a given position on the stream.
928 *****************************************************************************
929 * This one is used by the input and translate chronological position from
930 * input to logical position on the device.
931 *****************************************************************************/
932 static void DvdReadSeek( demux_t *p_demux, int i_block_offset )
934 demux_sys_t *p_sys = p_demux->p_sys;
936 unsigned int i_chapter = -1;
937 unsigned int i_cell = 0;
938 unsigned int i_vobu = 0;
939 unsigned int i_sub_cell = 0;
942 msg_Err( p_demux, "seek offset: %i", i_block_offset );
943 #define p_pgc p_sys->p_cur_pgc
944 #define p_vts p_sys->p_vts_file
947 p_sys->i_title_offset = i_block = i_block_offset;
948 while( i_block >= (int)p_pgc->cell_playback[i_cell].last_sector -
949 (int)p_pgc->cell_playback[i_cell].first_sector + 1 )
953 i_block -= p_pgc->cell_playback[i_cell].last_sector -
954 p_pgc->cell_playback[i_cell].first_sector + 1;
957 i_block += p_pgc->cell_playback[i_cell].first_sector;
965 pgc_id = p_vts->vts_ptt_srpt->title[
966 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
967 pgn = p_vts->vts_ptt_srpt->title[
968 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
970 i_tmp = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc->program_map[pgn-1];
972 } while( i_tmp <= i_cell );
974 if( i_chapter < p_sys->i_chapters &&
975 p_demux->info.i_seekpoint != i_chapter )
977 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
978 p_demux->info.i_seekpoint = i_chapter;
982 while( p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu] <= i_block )
988 while( p_vts->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
989 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
995 msg_Dbg( p_demux, "cell %d i_sub_cell %d chapter %d vobu %d "
996 "cell_sector %d vobu_sector %d sub_cell_sector %d",
997 i_cell, i_sub_cell, i_chapter, i_vobu,
998 p_sys->p_cur_pgc->cell_playback[i_cell].first_sector,
999 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu],
1000 p_vts->vts_c_adt->cell_adr_table[i_sub_cell - 1].start_sector);
1003 p_sys->i_cur_block = i_block;
1004 p_sys->i_next_vobu = p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu];
1005 p_sys->i_pack_len = p_sys->i_next_vobu - i_block;
1006 p_sys->i_cur_cell = i_cell;
1007 p_sys->i_chapter = i_chapter;
1008 DvdReadFindCell( p_demux );
1016 /*****************************************************************************
1018 *****************************************************************************/
1019 static void DvdReadHandleDSI( demux_t *p_demux, uint8_t *p_data )
1021 demux_sys_t *p_sys = p_demux->p_sys;
1023 navRead_DSI( &p_sys->dsi_pack, &p_data[DSI_START_BYTE] );
1026 * Determine where we go next. These values are the ones we mostly
1029 p_sys->i_cur_block = p_sys->dsi_pack.dsi_gi.nv_pck_lbn;
1032 * If we're not at the end of this cell, we can determine the next
1033 * VOBU to display using the VOBU_SRI information section of the
1034 * DSI. Using this value correctly follows the current angle,
1035 * avoiding the doubled scenes in The Matrix, and makes our life
1038 if( p_sys->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1040 switch( ( p_sys->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1043 /* Interleaved unit with no angle */
1044 if( p_sys->dsi_pack.sml_pbi.ilvu_sa != 0 )
1046 p_sys->i_next_vobu = p_sys->i_cur_block +
1047 p_sys->dsi_pack.sml_pbi.ilvu_sa;
1048 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1052 p_sys->i_next_vobu = p_sys->i_cur_block +
1053 p_sys->dsi_pack.dsi_gi.vobu_ea + 1;
1054 p_sys->i_pack_len = p_sys->dsi_pack.dsi_gi.vobu_ea;
1058 /* vobu is end of ilvu */
1059 if( p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address )
1061 p_sys->i_next_vobu = p_sys->i_cur_block +
1062 p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address;
1063 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1068 /* vobu is beginning of ilvu */
1072 /* entering interleaved section */
1074 /* non interleaved cells in interleaved section */
1076 p_sys->i_next_vobu = p_sys->i_cur_block +
1077 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1078 p_sys->i_pack_len = p_sys->dsi_pack.dsi_gi.vobu_ea;
1084 p_sys->i_cur_cell = p_sys->i_next_cell;
1085 DvdReadFindCell( p_demux );
1087 p_sys->i_pack_len = p_sys->dsi_pack.dsi_gi.vobu_ea;
1088 p_sys->i_next_vobu =
1089 p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1093 msg_Dbg( p_input, 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1094 p_sys->dsi_pack.dsi_gi.nv_pck_scr,
1095 p_sys->dsi_pack.dsi_gi.nv_pck_lbn,
1096 p_sys->dsi_pack.dsi_gi.vobu_ea,
1097 p_sys->dsi_pack.dsi_gi.vobu_vob_idn,
1098 p_sys->dsi_pack.dsi_gi.vobu_c_idn );
1100 msg_Dbg( p_input, 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1101 p_sys->dsi_pack.sml_pbi.category,
1102 p_sys->dsi_pack.sml_pbi.ilvu_ea,
1103 p_sys->dsi_pack.sml_pbi.ilvu_sa,
1104 p_sys->dsi_pack.sml_pbi.size );
1106 msg_Dbg( p_input, 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1107 p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1108 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle - 1 ].address,
1109 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle ].address);
1113 /*****************************************************************************
1115 *****************************************************************************/
1116 static void DvdReadFindCell( demux_t *p_demux )
1118 demux_sys_t *p_sys = p_demux->p_sys;
1124 #define cell p_sys->p_cur_pgc->cell_playback
1126 if( cell[p_sys->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1129 p_sys->i_next_cell = p_sys->i_cur_cell + p_sys->i_angle_nb;
1130 p_sys->i_cur_cell += p_sys->i_angle - 1;
1132 p_sys->i_cur_cell += p_sys->i_angle - 1;
1134 while( cell[p_sys->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1138 p_sys->i_next_cell = p_sys->i_cur_cell + i + 1;
1143 p_sys->i_next_cell = p_sys->i_cur_cell + 1;
1148 pgc_id = p_sys->p_vts_file->vts_ptt_srpt->title[
1149 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgcn;
1150 pgn = p_sys->p_vts_file->vts_ptt_srpt->title[
1151 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgn;
1152 p_pgc = p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
1154 if( p_pgc->program_map[pgn - 1] <= p_sys->i_cur_cell )
1158 if( p_sys->i_chapter < p_sys->i_chapters &&
1159 p_demux->info.i_seekpoint != p_sys->i_chapter )
1161 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1162 p_demux->info.i_seekpoint = p_sys->i_chapter;
1167 /*****************************************************************************
1168 * DemuxTitles: get the titles/chapters structure
1169 *****************************************************************************/
1170 static void DemuxTitles( demux_t *p_demux,
1171 int *pi_title, int *pi_chapter, int *pi_angle )
1173 demux_sys_t *p_sys = p_demux->p_sys;
1179 /* Find out number of titles/chapters */
1180 #define tt_srpt p_sys->p_vmg_file->tt_srpt
1182 i_titles = tt_srpt->nr_of_srpts;
1183 msg_Dbg( p_demux, "number of titles: %d", i_titles );
1185 for( i = 0; i < i_titles; i++ )
1187 int32_t i_chapters = 0;
1190 i_chapters = tt_srpt->title[i].nr_of_ptts;
1191 msg_Dbg( p_demux, "title %d has %d chapters", i, i_chapters );
1193 t = vlc_input_title_New();
1194 t->psz_name = malloc( strlen( _("Title %i") ) + 20 );
1195 sprintf( t->psz_name, _("Title %i"), i + 1 );
1197 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
1199 s = vlc_seekpoint_New();
1200 s->psz_name = malloc( strlen( _("Chapter %i") ) + 20 );
1201 sprintf( s->psz_name, _("Chapter %i"), j + 1 );
1202 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1205 TAB_APPEND( p_sys->i_titles, p_sys->titles, t );
1208 /* Set forced title/chapter/angle */
1209 *pi_title = (*pi_title >= 1 && *pi_title <= i_titles) ? *pi_title : 1;
1210 *pi_chapter = (*pi_chapter >= 0 && *pi_chapter <
1211 tt_srpt->title[*pi_title].nr_of_ptts) ? *pi_chapter : 0;
1216 /*****************************************************************************
1217 * ParseCL: parse command line
1218 *****************************************************************************/
1219 static char *ParseCL( vlc_object_t *p_this, char *psz_name, vlc_bool_t b_force,
1220 int *i_title, int *i_chapter, int *i_angle )
1222 char *psz_parser, *psz_source, *psz_next;
1224 psz_source = strdup( psz_name );
1225 if( psz_source == NULL ) return NULL;
1231 /* Start with the end, because you could have :
1232 * dvdnav:/Volumes/my@toto/VIDEO_TS@1,1
1233 * (yes, this is kludgy). */
1234 for( psz_parser = psz_source + strlen(psz_source) - 1;
1235 psz_parser >= psz_source && *psz_parser != '@';
1238 if( psz_parser >= psz_source && *psz_parser == '@' )
1244 *i_title = (int)strtol( psz_parser, &psz_next, 10 );
1247 psz_parser = psz_next + 1;
1248 *i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
1251 *i_angle = (int)strtol( psz_next + 1, NULL, 10 );
1256 *i_title = *i_title >= 0 ? *i_title : 0;
1257 *i_chapter = *i_chapter ? *i_chapter : 0;
1258 *i_angle = *i_angle ? *i_angle : 1;
1267 psz_source = config_GetPsz( p_this, "dvd" );
1268 if( !psz_source ) return NULL;
1272 if( psz_source[0] && psz_source[1] == ':' &&
1273 psz_source[2] == '\\' && psz_source[3] == '\0' )
1275 psz_source[2] = '\0';
1279 msg_Dbg( p_this, "dvdroot=%s title=%d chapter=%d angle=%d",
1280 psz_source, *i_title, *i_chapter, *i_angle );