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;
116 int i_chapter, i_chapters;
117 int i_angle, i_angles;
119 tt_srpt_t *p_tt_srpt;
128 /* Current title start/end blocks */
129 int i_title_start_block;
130 int i_title_end_block;
134 int i_title_start_cell;
135 int i_title_end_cell;
140 ps_track_t tk[PS_TK_COUNT];
144 input_title_t **titles;
153 static char *ParseCL( vlc_object_t *, char *, vlc_bool_t, int *, int *, int *);
155 static int Control ( demux_t *, int, va_list );
156 static int Demux ( demux_t * );
157 static int DemuxBlock( demux_t *, uint8_t *, int );
159 static void DemuxTitles( demux_t *, int *, int *, int * );
160 static void ESNew( demux_t *, int, int );
162 static int DvdReadSetArea ( demux_t *, int, int, int );
163 static void DvdReadSeek ( demux_t *, int );
164 static void DvdReadHandleDSI( demux_t *, uint8_t * );
165 static void DvdReadFindCell ( demux_t * );
167 /*****************************************************************************
169 *****************************************************************************/
170 static int Open( vlc_object_t *p_this )
172 demux_t *p_demux = (demux_t*)p_this;
174 int i_title, i_chapter, i_angle;
176 char *psz_dvdcss_env;
177 dvd_reader_t *p_dvdread;
178 ifo_handle_t *p_vmg_file;
180 psz_name = ParseCL( VLC_OBJECT(p_demux), p_demux->psz_path, VLC_TRUE,
181 &i_title, &i_chapter, &i_angle );
187 /* Override environment variable DVDCSS_METHOD with config option
188 * (FIXME: this creates a small memory leak) */
189 psz_dvdcss_env = config_GetPsz( p_demux, "dvdread-css-method" );
190 if( psz_dvdcss_env && *psz_dvdcss_env )
194 psz_env = malloc( strlen("DVDCSS_METHOD=") +
195 strlen( psz_dvdcss_env ) + 1 );
199 free( psz_dvdcss_env );
203 sprintf( psz_env, "%s%s", "DVDCSS_METHOD=", psz_dvdcss_env );
207 if( psz_dvdcss_env ) free( psz_dvdcss_env );
210 if( !(p_dvdread = DVDOpen( psz_name )) )
212 msg_Err( p_demux, "DVDRead cannot open source: %s", psz_name );
218 /* Ifo allocation & initialisation */
219 if( !( p_vmg_file = ifoOpen( p_dvdread, 0 ) ) )
221 msg_Warn( p_demux, "cannot open VMG info" );
224 msg_Dbg( p_demux, "VMG opened" );
226 /* Fill p_demux field */
227 p_demux->pf_demux = Demux;
228 p_demux->pf_control = Control;
229 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
230 memset( p_sys, 0, sizeof( demux_sys_t ) );
232 ps_track_init( p_sys->tk );
233 p_sys->i_aspect = -1;
234 p_sys->i_mux_rate = 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_angle = i_angle;
244 DemuxTitles( p_demux, &i_title, &i_chapter, &i_angle );
246 DvdReadSetArea( p_demux, i_title, i_chapter, i_angle );
248 /* Update default_pts to a suitable value for dvdread access */
249 var_Create( p_demux, "dvdread-caching",
250 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
255 /*****************************************************************************
257 *****************************************************************************/
258 static void Close( vlc_object_t *p_this )
260 demux_t *p_demux = (demux_t*)p_this;
261 demux_sys_t *p_sys = p_demux->p_sys;
264 for( i = 0; i < PS_TK_COUNT; i++ )
266 ps_track_t *tk = &p_sys->tk[i];
269 es_format_Clean( &tk->fmt );
270 if( tk->es ) es_out_Del( p_demux->out, tk->es );
274 /* Close libdvdread */
275 if( p_sys->p_title ) DVDCloseFile( p_sys->p_title );
276 if( p_sys->p_vts_file ) ifoClose( p_sys->p_vts_file );
277 if( p_sys->p_vmg_file ) ifoClose( p_sys->p_vmg_file );
278 DVDClose( p_sys->p_dvdread );
283 /*****************************************************************************
285 *****************************************************************************/
286 static int Control( demux_t *p_demux, int i_query, va_list args )
288 demux_sys_t *p_sys = p_demux->p_sys;
292 input_title_t ***ppp_title;
298 case DEMUX_GET_POSITION:
300 pf = (double*) va_arg( args, double* );
302 if( p_sys->i_title_blocks > 0 )
303 *pf = (double)p_sys->i_title_offset / p_sys->i_title_blocks;
309 case DEMUX_SET_POSITION:
311 f = (double)va_arg( args, double );
313 DvdReadSeek( p_demux, f * p_sys->i_title_blocks );
318 pi64 = (int64_t*)va_arg( args, int64_t * );
319 if( p_sys->i_mux_rate > 0 )
321 *pi64 = (int64_t)1000000 * DVD_VIDEO_LB_LEN *
322 p_sys->i_title_offset / 50 / p_sys->i_mux_rate;
328 case DEMUX_GET_LENGTH:
329 pi64 = (int64_t*)va_arg( args, int64_t * );
330 if( p_sys->i_mux_rate > 0 )
332 *pi64 = (int64_t)1000000 * DVD_VIDEO_LB_LEN *
333 p_sys->i_title_blocks / 50 / p_sys->i_mux_rate;
339 /* Special for access_demux */
340 case DEMUX_CAN_PAUSE:
341 case DEMUX_CAN_CONTROL_PACE:
343 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
347 case DEMUX_SET_PAUSE_STATE:
350 case DEMUX_GET_TITLE_INFO:
351 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
352 pi_int = (int*)va_arg( args, int* );
354 /* Duplicate title infos */
355 *pi_int = p_sys->i_titles;
356 *ppp_title = malloc( sizeof(input_title_t **) * p_sys->i_titles );
357 for( i = 0; i < p_sys->i_titles; i++ )
359 (*ppp_title)[i] = vlc_input_title_Duplicate(p_sys->titles[i]);
363 case DEMUX_SET_TITLE:
364 i = (int)va_arg( args, int );
365 if( DvdReadSetArea( p_demux, i, 0, -1 ) != VLC_SUCCESS )
367 msg_Warn( p_demux, "cannot set title/chapter" );
370 p_demux->info.i_update |=
371 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
372 p_demux->info.i_title = i;
373 p_demux->info.i_seekpoint = 0;
376 case DEMUX_SET_SEEKPOINT:
377 i = (int)va_arg( args, int );
378 if( DvdReadSetArea( p_demux, -1, i, -1 ) != VLC_SUCCESS )
380 msg_Warn( p_demux, "cannot set title/chapter" );
383 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
384 p_demux->info.i_seekpoint = i;
387 case DEMUX_GET_PTS_DELAY:
388 pi64 = (int64_t*)va_arg( args, int64_t * );
389 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdread-caching" )*1000;
392 /* TODO implement others */
398 /*****************************************************************************
400 *****************************************************************************/
401 static int Demux( demux_t *p_demux )
403 demux_sys_t *p_sys = p_demux->p_sys;
405 uint8_t p_buffer[DVD_VIDEO_LB_LEN * DVD_BLOCK_READ_ONCE];
406 int i_blocks_once, i_read;
410 * Playback by cell in this pgc, starting at the cell for our chapter.
414 * Check end of pack, and select the following one
416 if( !p_sys->i_pack_len )
418 /* Read NAV packet */
419 if( DVDReadBlocks( p_sys->p_title, p_sys->i_next_vobu,
422 msg_Err( p_demux, "read failed for block %d", p_sys->i_next_vobu );
426 /* Basic check to be sure we don't have a empty title
427 * go to next title if so */
428 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
430 /* Parse the contained dsi packet */
431 DvdReadHandleDSI( p_demux, p_buffer );
434 if( p_sys->i_next_vobu > p_sys->i_title_end_block )
436 if( p_sys->i_title + 1 >= p_sys->i_titles ) return 0; /* EOF */
438 DvdReadSetArea( p_demux, p_sys->i_title + 1, 0, -1 );
441 if( p_sys->i_pack_len >= 1024 )
443 msg_Err( p_demux, "i_pack_len >= 1024. This shouldn't happen!" );
447 /* FIXME: Ugly kludge: we send the pack block to the input for it
448 * sometimes has a zero scr and restart the sync */
449 p_sys->i_cur_block++;
450 p_sys->i_title_offset++;
452 DemuxBlock( p_demux, p_buffer, DVD_VIDEO_LB_LEN );
455 if( p_sys->i_cur_block > p_sys->i_title_end_block )
457 if( p_sys->i_title + 1 >= p_sys->i_titles ) return 0; /* EOF */
459 DvdReadSetArea( p_demux, p_sys->i_title + 1, 0, -1 );
465 i_blocks_once = __MIN( p_sys->i_pack_len, DVD_BLOCK_READ_ONCE );
466 p_sys->i_pack_len -= i_blocks_once;
469 i_read = DVDReadBlocks( p_sys->p_title, p_sys->i_cur_block,
470 i_blocks_once, p_buffer );
471 if( i_read != i_blocks_once )
473 msg_Err( p_demux, "read failed for %d/%d blocks at 0x%02x",
474 i_read, i_blocks_once, p_sys->i_cur_block );
478 p_sys->i_cur_block += i_read;
479 p_sys->i_title_offset += i_read;
482 msg_Dbg( p_demux, "i_blocks: %d len: %d current: 0x%02x",
483 i_read, p_sys->i_pack_len, p_sys->i_cur_block );
486 for( i = 0; i < i_read; i++ )
488 DemuxBlock( p_demux, p_buffer + i * DVD_VIDEO_LB_LEN,
497 /*****************************************************************************
498 * DemuxBlock: demux a given block
499 *****************************************************************************/
500 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
502 demux_sys_t *p_sys = p_demux->p_sys;
505 while( p < &pkt[i_pkt] )
507 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
515 p_pkt = block_New( p_demux, i_size );
516 memcpy( p_pkt->p_buffer, p, i_size);
518 /* Parse it and send it */
519 switch( 0x100 | p[3] )
528 msg_Warn( p_demux, "received a PSM packet" );
530 else if( p[3] == 0xbb )
532 msg_Warn( p_demux, "received a SYSTEM packet" );
535 block_Release( p_pkt );
542 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
544 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
545 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
547 block_Release( p_pkt );
552 int i_id = ps_pkt_id( p_pkt );
555 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
559 ESNew( p_demux, i_id, 0 );
561 if( tk->b_seen && tk->es &&
562 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
564 es_out_Send( p_demux->out, tk->es, p_pkt );
568 block_Release( p_pkt );
573 block_Release( p_pkt );
585 /*****************************************************************************
586 * ESNew: register a new elementary stream
587 *****************************************************************************/
588 static void ESNew( demux_t *p_demux, int i_id, int i_lang )
590 demux_sys_t *p_sys = p_demux->p_sys;
591 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
592 char psz_language[3];
594 if( tk->b_seen ) return;
596 if( ps_track_fill( tk, i_id ) )
598 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
602 psz_language[0] = psz_language[1] = psz_language[2] = 0;
603 if( i_lang && i_lang != 0xffff )
605 psz_language[0] = (i_lang >> 8)&0xff;
606 psz_language[1] = (i_lang )&0xff;
610 if( tk->fmt.i_cat == VIDEO_ES )
612 if( p_sys->i_aspect >= 0 )
614 tk->fmt.video.i_aspect = p_sys->i_aspect;
617 else if( tk->fmt.i_cat == AUDIO_ES )
620 /* find the audio number PLEASE find another way */
621 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
625 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
629 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
633 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
638 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
640 else if( tk->fmt.i_cat == SPU_ES )
643 tk->fmt.subs.spu.palette[0] = 0xBeef;
644 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
645 16 * sizeof( uint32_t ) );
647 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
650 tk->es = es_out_Add( p_demux->out, &tk->fmt );
651 tk->b_seen = VLC_TRUE;
654 /*****************************************************************************
655 * DvdReadSetArea: initialize input data for title x, chapter y.
656 * It should be called for each user navigation request.
657 *****************************************************************************
658 * Take care that i_title and i_chapter start from 0.
659 *****************************************************************************/
660 static int DvdReadSetArea( demux_t *p_demux, int i_title, int i_chapter,
663 demux_sys_t *p_sys = p_demux->p_sys;
664 int pgc_id = 0, pgn = 0;
667 #define p_pgc p_sys->p_cur_pgc
668 #define p_vmg p_sys->p_vmg_file
669 #define p_vts p_sys->p_vts_file
671 if( i_title >= 0 && i_title < p_sys->i_titles &&
672 i_title != p_sys->i_title )
674 int i_start_cell, i_end_cell;
676 if( p_sys->p_title != NULL ) DVDCloseFile( p_sys->p_title );
677 if( p_vts != NULL ) ifoClose( p_vts );
678 p_sys->i_title = i_title;
681 * We have to load all title information
683 msg_Dbg( p_demux, "open VTS %d, for title %d",
684 p_vmg->tt_srpt->title[i_title].title_set_nr, i_title + 1 );
687 if( !( p_vts = ifoOpen( p_sys->p_dvdread,
688 p_vmg->tt_srpt->title[i_title].title_set_nr ) ) )
690 msg_Err( p_demux, "fatal error in vts ifo" );
694 /* Title position inside the selected vts */
695 p_sys->i_ttn = p_vmg->tt_srpt->title[i_title].vts_ttn;
697 /* Find title start/end */
698 pgc_id = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgcn;
699 pgn = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgn;
700 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
702 p_sys->i_title_start_cell =
703 i_start_cell = p_pgc->program_map[pgn - 1] - 1;
704 p_sys->i_title_start_block =
705 p_pgc->cell_playback[i_start_cell].first_sector;
707 p_sys->i_title_end_cell =
708 i_end_cell = p_pgc->nr_of_cells - 1;
709 p_sys->i_title_end_block =
710 p_pgc->cell_playback[i_end_cell].last_sector;
712 p_sys->i_title_offset = 0;
714 p_sys->i_title_blocks = 0;
715 for( i = i_start_cell; i <= i_end_cell; i++ )
717 p_sys->i_title_blocks += p_pgc->cell_playback[i].last_sector -
718 p_pgc->cell_playback[i].first_sector + 1;
721 msg_Dbg( p_demux, "title %d vts_title %d pgc %d pgn %d "
722 "start %d end %d blocks: %d",
723 i_title + 1, p_sys->i_ttn, pgc_id, pgn,
724 p_sys->i_title_start_block, p_sys->i_title_end_block,
725 p_sys->i_title_blocks );
728 * Set properties for current chapter
730 p_sys->i_chapter = 0;
732 p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].nr_of_ptts;
734 pgc_id = p_vts->vts_ptt_srpt->title[
735 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgcn;
736 pgn = p_vts->vts_ptt_srpt->title[
737 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgn;
739 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
740 p_sys->i_pack_len = 0;
742 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
743 DvdReadFindCell( p_demux );
745 p_sys->i_next_vobu = p_sys->i_cur_block =
746 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
751 p_sys->i_angles = p_vmg->tt_srpt->title[i_title].nr_of_angles;
752 if( p_sys->i_angle > p_sys->i_angles ) p_sys->i_angle = 1;
755 * We've got enough info, time to open the title set data.
757 if( !( p_sys->p_title = DVDOpenFile( p_sys->p_dvdread,
758 p_vmg->tt_srpt->title[i_title].title_set_nr,
759 DVD_READ_TITLE_VOBS ) ) )
761 msg_Err( p_demux, "cannot open title (VTS_%02d_1.VOB)",
762 p_vmg->tt_srpt->title[i_title].title_set_nr );
766 //IfoPrintTitle( p_demux );
769 * Destroy obsolete ES by reinitializing program 0
770 * and find all ES in title with ifo data
772 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
774 for( i = 0; i < PS_TK_COUNT; i++ )
776 ps_track_t *tk = &p_sys->tk[i];
779 es_format_Clean( &tk->fmt );
780 if( tk->es ) es_out_Del( p_demux->out, tk->es );
782 tk->b_seen = VLC_FALSE;
785 if( p_demux->info.i_title != i_title )
787 p_demux->info.i_update |=
788 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
789 p_demux->info.i_title = i_title;
790 p_demux->info.i_seekpoint = 0;
793 /* TODO: re-add angles */
796 ESNew( p_demux, 0xe0, 0 ); /* Video, FIXME ? */
797 p_sys->i_aspect = p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio;
799 #define audio_control \
800 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
802 /* Audio ES, in the order they appear in the .ifo */
803 for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
808 //IfoPrintAudio( p_demux, i );
810 /* Audio channel is active if first byte is 0x80 */
811 if( audio_control & 0x8000 )
813 i_position = ( audio_control & 0x7F00 ) >> 8;
815 msg_Dbg( p_demux, "audio position %d", i_position );
816 switch( p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format )
819 i_id = (0x80 + i_position) | 0xbd00;
822 case 0x03: /* MPEG audio */
823 i_id = 0xc000 + i_position;
825 case 0x04: /* LPCM */
826 i_id = (0xa0 + i_position) | 0xbd00;
829 i_id = (0x88 + i_position) | 0xbd00;
833 msg_Err( p_demux, "unknown audio type %.2x",
834 p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format );
837 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
838 vts_audio_attr[i - 1].lang_code );
843 #define spu_palette \
844 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette
846 memcpy( p_sys->clut, spu_palette, 16 * sizeof( uint32_t ) );
848 #define spu_control \
849 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
852 for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
857 //IfoPrintSpu( p_sys, i );
858 msg_Dbg( p_demux, "spu %d 0x%02x", i, spu_control );
860 if( spu_control & 0x80000000 )
862 /* there are several streams for one spu */
863 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
866 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
868 case 1: /* letterbox */
869 i_position = spu_control & 0xff;
871 case 2: /* pan&scan */
872 i_position = ( spu_control >> 8 ) & 0xff;
874 default: /* widescreen */
875 i_position = ( spu_control >> 16 ) & 0xff;
882 i_position = ( spu_control >> 24 ) & 0x7F;
885 i_id = (0x20 + i_position) | 0xbd00;
887 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
888 vts_subp_attr[i - 1].lang_code );
894 else if( i_title != -1 && i_title != p_sys->i_title )
897 return VLC_EGENERIC; /* Couldn't set title */
904 if( i_chapter >= 0 && i_chapter <= p_sys->i_chapters )
906 pgc_id = p_vts->vts_ptt_srpt->title[
907 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
908 pgn = p_vts->vts_ptt_srpt->title[
909 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
911 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
913 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
914 p_sys->i_chapter = i_chapter;
915 DvdReadFindCell( p_demux );
917 p_sys->i_title_offset = 0;
918 for( i = p_sys->i_title_start_cell; i < p_sys->i_cur_cell; i++ )
920 p_sys->i_title_offset += p_pgc->cell_playback[i].last_sector -
921 p_pgc->cell_playback[i].first_sector + 1;
924 p_sys->i_pack_len = 0;
925 p_sys->i_next_vobu = p_sys->i_cur_block =
926 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
928 if( p_demux->info.i_seekpoint != i_chapter )
930 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
931 p_demux->info.i_seekpoint = i_chapter;
934 else if( i_chapter != -1 )
937 return VLC_EGENERIC; /* Couldn't set chapter */
947 /*****************************************************************************
948 * DvdReadSeek : Goes to a given position on the stream.
949 *****************************************************************************
950 * This one is used by the input and translate chronological position from
951 * input to logical position on the device.
952 *****************************************************************************/
953 static void DvdReadSeek( demux_t *p_demux, int i_block_offset )
955 demux_sys_t *p_sys = p_demux->p_sys;
962 #define p_pgc p_sys->p_cur_pgc
963 #define p_vts p_sys->p_vts_file
966 i_block = i_block_offset;
967 for( i_cell = p_sys->i_title_start_cell;
968 i_cell <= p_sys->i_title_end_cell; i_cell++ )
970 if( i_block < (int)p_pgc->cell_playback[i_cell].last_sector -
971 (int)p_pgc->cell_playback[i_cell].first_sector + 1 ) break;
973 i_block -= (p_pgc->cell_playback[i_cell].last_sector -
974 p_pgc->cell_playback[i_cell].first_sector + 1);
976 if( i_cell > p_sys->i_title_end_cell )
978 msg_Err( p_demux, "couldn't find cell for block %i", i_block_offset );
981 i_block += p_pgc->cell_playback[i_cell].first_sector;
982 p_sys->i_title_offset = i_block_offset;
985 for( i_chapter = 0; i_chapter < p_sys->i_chapters; i_chapter++ )
987 int pgc_id, pgn, i_tmp;
989 pgc_id = p_vts->vts_ptt_srpt->title[
990 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
991 pgn = p_vts->vts_ptt_srpt->title[
992 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
994 i_tmp = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc->program_map[pgn-1];
996 if( i_tmp > i_cell ) break;
999 if( i_chapter < p_sys->i_chapters &&
1000 p_demux->info.i_seekpoint != i_chapter )
1002 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1003 p_demux->info.i_seekpoint = i_chapter;
1007 while( (int)p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu] <= i_block )
1013 while( p_vts->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1014 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1020 msg_Dbg( p_demux, "cell %d i_sub_cell %d chapter %d vobu %d "
1021 "cell_sector %d vobu_sector %d sub_cell_sector %d",
1022 i_cell, i_sub_cell, i_chapter, i_vobu,
1023 p_sys->p_cur_pgc->cell_playback[i_cell].first_sector,
1024 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu],
1025 p_vts->vts_c_adt->cell_adr_table[i_sub_cell - 1].start_sector);
1028 p_sys->i_cur_block = i_block;
1029 p_sys->i_next_vobu = p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu];
1030 p_sys->i_pack_len = p_sys->i_next_vobu - i_block;
1031 p_sys->i_cur_cell = i_cell;
1032 p_sys->i_chapter = i_chapter;
1033 DvdReadFindCell( p_demux );
1041 /*****************************************************************************
1043 *****************************************************************************/
1044 static void DvdReadHandleDSI( demux_t *p_demux, uint8_t *p_data )
1046 demux_sys_t *p_sys = p_demux->p_sys;
1048 navRead_DSI( &p_sys->dsi_pack, &p_data[DSI_START_BYTE] );
1051 * Determine where we go next. These values are the ones we mostly
1054 p_sys->i_cur_block = p_sys->dsi_pack.dsi_gi.nv_pck_lbn;
1057 * If we're not at the end of this cell, we can determine the next
1058 * VOBU to display using the VOBU_SRI information section of the
1059 * DSI. Using this value correctly follows the current angle,
1060 * avoiding the doubled scenes in The Matrix, and makes our life
1063 if( p_sys->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL )
1065 switch( ( p_sys->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1068 /* Interleaved unit with no angle */
1069 if( p_sys->dsi_pack.sml_pbi.ilvu_sa != 0 )
1071 p_sys->i_next_vobu = p_sys->i_cur_block +
1072 p_sys->dsi_pack.sml_pbi.ilvu_sa;
1073 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1077 p_sys->i_next_vobu = p_sys->i_cur_block +
1078 p_sys->dsi_pack.dsi_gi.vobu_ea + 1;
1079 p_sys->i_pack_len = p_sys->dsi_pack.dsi_gi.vobu_ea;
1083 /* vobu is end of ilvu */
1084 if( p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address )
1086 p_sys->i_next_vobu = p_sys->i_cur_block +
1087 p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address;
1088 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1093 /* vobu is beginning of ilvu */
1097 /* entering interleaved section */
1099 /* non interleaved cells in interleaved section */
1101 p_sys->i_next_vobu = p_sys->i_cur_block +
1102 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1103 p_sys->i_pack_len = p_sys->dsi_pack.dsi_gi.vobu_ea;
1109 p_sys->i_cur_cell = p_sys->i_next_cell;
1110 DvdReadFindCell( p_demux );
1112 p_sys->i_pack_len = p_sys->dsi_pack.dsi_gi.vobu_ea;
1113 p_sys->i_next_vobu =
1114 p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1118 msg_Dbg( p_demux, 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1119 p_sys->dsi_pack.dsi_gi.nv_pck_scr,
1120 p_sys->dsi_pack.dsi_gi.nv_pck_lbn,
1121 p_sys->dsi_pack.dsi_gi.vobu_ea,
1122 p_sys->dsi_pack.dsi_gi.vobu_vob_idn,
1123 p_sys->dsi_pack.dsi_gi.vobu_c_idn );
1125 msg_Dbg( p_demux, 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1126 p_sys->dsi_pack.sml_pbi.category,
1127 p_sys->dsi_pack.sml_pbi.ilvu_ea,
1128 p_sys->dsi_pack.sml_pbi.ilvu_sa,
1129 p_sys->dsi_pack.sml_pbi.size );
1131 msg_Dbg( p_demux, 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1132 p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1133 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle - 1 ].address,
1134 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle ].address);
1138 /*****************************************************************************
1140 *****************************************************************************/
1141 static void DvdReadFindCell( demux_t *p_demux )
1143 demux_sys_t *p_sys = p_demux->p_sys;
1149 #define cell p_sys->p_cur_pgc->cell_playback
1151 if( cell[p_sys->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1153 p_sys->i_cur_cell += p_sys->i_angle - 1;
1155 while( cell[p_sys->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1159 p_sys->i_next_cell = p_sys->i_cur_cell + i + 1;
1163 p_sys->i_next_cell = p_sys->i_cur_cell + 1;
1168 pgc_id = p_sys->p_vts_file->vts_ptt_srpt->title[
1169 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgcn;
1170 pgn = p_sys->p_vts_file->vts_ptt_srpt->title[
1171 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgn;
1172 p_pgc = p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
1174 if( p_pgc->program_map[pgn - 1] <= p_sys->i_cur_cell )
1178 if( p_sys->i_chapter < p_sys->i_chapters &&
1179 p_demux->info.i_seekpoint != p_sys->i_chapter )
1181 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1182 p_demux->info.i_seekpoint = p_sys->i_chapter;
1187 /*****************************************************************************
1188 * DemuxTitles: get the titles/chapters structure
1189 *****************************************************************************/
1190 static void DemuxTitles( demux_t *p_demux,
1191 int *pi_title, int *pi_chapter, int *pi_angle )
1193 demux_sys_t *p_sys = p_demux->p_sys;
1199 /* Find out number of titles/chapters */
1200 #define tt_srpt p_sys->p_vmg_file->tt_srpt
1202 i_titles = tt_srpt->nr_of_srpts;
1203 msg_Dbg( p_demux, "number of titles: %d", i_titles );
1205 for( i = 0; i < i_titles; i++ )
1207 int32_t i_chapters = 0;
1210 i_chapters = tt_srpt->title[i].nr_of_ptts;
1211 msg_Dbg( p_demux, "title %d has %d chapters", i, i_chapters );
1213 t = vlc_input_title_New();
1214 t->psz_name = malloc( strlen( _("Title %i") ) + 20 );
1215 sprintf( t->psz_name, _("Title %i"), i + 1 );
1217 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
1219 s = vlc_seekpoint_New();
1220 s->psz_name = malloc( strlen( _("Chapter %i") ) + 20 );
1221 sprintf( s->psz_name, _("Chapter %i"), j + 1 );
1222 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1225 TAB_APPEND( p_sys->i_titles, p_sys->titles, t );
1228 /* Set forced title/chapter/angle */
1229 *pi_title = (*pi_title >= 0 && *pi_title < i_titles) ? *pi_title : 0;
1230 *pi_chapter = (*pi_chapter >= 0 && *pi_chapter <
1231 tt_srpt->title[*pi_title].nr_of_ptts) ? *pi_chapter : 0;
1236 /*****************************************************************************
1237 * ParseCL: parse command line. Titles, chapters and angles start from 1.
1238 *****************************************************************************/
1239 static char *ParseCL( vlc_object_t *p_this, char *psz_name, vlc_bool_t b_force,
1240 int *i_title, int *i_chapter, int *i_angle )
1242 char *psz_parser, *psz_source, *psz_next;
1244 psz_source = strdup( psz_name );
1245 if( psz_source == NULL ) return NULL;
1251 /* Start with the end, because you could have :
1252 * dvdnav:/Volumes/my@toto/VIDEO_TS@1,1
1253 * (yes, this is kludgy). */
1254 for( psz_parser = psz_source + strlen(psz_source) - 1;
1255 psz_parser >= psz_source && *psz_parser != '@';
1258 if( psz_parser >= psz_source && *psz_parser == '@' )
1264 *i_title = (int)strtol( psz_parser, &psz_next, 10 );
1267 psz_parser = psz_next + 1;
1268 *i_chapter = (int)strtol( psz_parser, &psz_next, 10 );
1271 *i_angle = (int)strtol( psz_next + 1, NULL, 10 );
1276 *i_title = *i_title > 0 ? *i_title : 1;
1277 *i_chapter = *i_chapter > 0 ? *i_chapter : 1;
1278 *i_angle = *i_angle > 0 ? *i_angle : 1;
1287 psz_source = config_GetPsz( p_this, "dvd" );
1288 if( !psz_source ) return NULL;
1292 if( psz_source[0] && psz_source[1] == ':' &&
1293 psz_source[2] == '\\' && psz_source[3] == '\0' )
1295 psz_source[2] = '\0';
1299 msg_Dbg( p_this, "dvdroot=%s title=%d chapter=%d angle=%d",
1300 psz_source, *i_title, *i_chapter, *i_angle );
1302 /* Get back to a 0-based offset */