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 ANGLE_TEXT N_("DVD angle")
58 #define ANGLE_LONGTEXT N_( \
59 "Allows you to select the default DVD angle." )
61 #define CACHING_TEXT N_("Caching value in ms")
62 #define CACHING_LONGTEXT N_( \
63 "Allows you to modify the default caching value for DVDread streams. " \
64 "This value should be set in millisecond units." )
66 #define CSSMETHOD_TEXT N_("Method used by libdvdcss for decryption")
67 #define CSSMETHOD_LONGTEXT N_( \
68 "Set the method used by libdvdcss for key decryption.\n" \
69 "title: decrypted title key is guessed from the encrypted sectors of " \
70 "the stream. Thus it should work with a file as well as the " \
71 "DVD device. But it sometimes takes much time to decrypt a title " \
72 "key and may even fail. With this method, the key is only checked "\
73 "at the beginning of each title, so it won't work if the key " \
74 "changes in the middle of a title.\n" \
75 "disc: the disc key is first cracked, then all title keys can be " \
76 "decrypted instantly, which allows us to check them often.\n" \
77 "key: the same as \"disc\" if you don't have a file with player keys " \
78 "at compilation time. If you do, the decryption of the disc key " \
79 "will be faster with this method. It is the one that was used by " \
81 "The default method is: key.")
83 static char *psz_css_list[] = { "title", "disc", "key" };
84 static char *psz_css_list_text[] = { N_("title"), N_("Disc"), N_("Key") };
86 static int Open ( vlc_object_t * );
87 static void Close( vlc_object_t * );
90 set_description( _("DVDRead Input") );
91 add_integer( "dvdread-angle", 1, NULL, ANGLE_TEXT,
92 ANGLE_LONGTEXT, VLC_FALSE );
93 add_integer( "dvdread-caching", DEFAULT_PTS_DELAY / 1000, NULL,
94 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
95 add_string( "dvdread-css-method", NULL, NULL, CSSMETHOD_TEXT,
96 CSSMETHOD_LONGTEXT, VLC_TRUE );
97 change_string_list( psz_css_list, psz_css_list_text, 0 );
98 set_capability( "access_demux", 0 );
99 add_shortcut( "dvd" );
100 add_shortcut( "dvdread" );
101 add_shortcut( "dvdsimple" );
102 set_callbacks( Open, Close );
105 /* how many blocks DVDRead will read in each loop */
106 #define DVD_BLOCK_READ_ONCE 4
108 /*****************************************************************************
110 *****************************************************************************/
115 dvd_reader_t *p_dvdread;
118 ifo_handle_t *p_vmg_file;
119 ifo_handle_t *p_vts_file;
122 int i_chapter, i_chapters;
123 int i_angle, i_angles;
125 tt_srpt_t *p_tt_srpt;
134 /* Current title start/end blocks */
135 int i_title_start_block;
136 int i_title_end_block;
140 int i_title_start_cell;
141 int i_title_end_cell;
146 ps_track_t tk[PS_TK_COUNT];
150 input_title_t **titles;
159 static int Control ( demux_t *, int, va_list );
160 static int Demux ( demux_t * );
161 static int DemuxBlock( demux_t *, uint8_t *, int );
163 static void DemuxTitles( demux_t *, int * );
164 static void ESNew( demux_t *, int, int );
166 static int DvdReadSetArea ( demux_t *, int, int, int );
167 static void DvdReadSeek ( demux_t *, int );
168 static void DvdReadHandleDSI( demux_t *, uint8_t * );
169 static void DvdReadFindCell ( demux_t * );
171 /*****************************************************************************
173 *****************************************************************************/
174 static int Open( vlc_object_t *p_this )
176 demux_t *p_demux = (demux_t*)p_this;
179 char *psz_dvdcss_env;
180 dvd_reader_t *p_dvdread;
181 ifo_handle_t *p_vmg_file;
184 if( !p_demux->psz_path || !*p_demux->psz_path )
186 /* Only when selected */
187 if( !p_this->b_force ) return VLC_EGENERIC;
189 psz_name = var_CreateGetString( p_this, "dvd" );
190 if( !psz_name || !*psz_name )
192 if( psz_name ) free( psz_name );
196 else psz_name = strdup( p_demux->psz_path );
199 if( psz_name[0] && psz_name[1] == ':' &&
200 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
203 /* Override environment variable DVDCSS_METHOD with config option
204 * (FIXME: this creates a small memory leak) */
205 psz_dvdcss_env = config_GetPsz( p_demux, "dvdread-css-method" );
206 if( psz_dvdcss_env && *psz_dvdcss_env )
210 psz_env = malloc( strlen("DVDCSS_METHOD=") +
211 strlen( psz_dvdcss_env ) + 1 );
214 free( psz_dvdcss_env );
218 sprintf( psz_env, "%s%s", "DVDCSS_METHOD=", psz_dvdcss_env );
222 if( psz_dvdcss_env ) free( psz_dvdcss_env );
225 if( !(p_dvdread = DVDOpen( psz_name )) )
227 msg_Err( p_demux, "DVDRead cannot open source: %s", psz_name );
233 /* Ifo allocation & initialisation */
234 if( !( p_vmg_file = ifoOpen( p_dvdread, 0 ) ) )
236 msg_Warn( p_demux, "cannot open VMG info" );
239 msg_Dbg( p_demux, "VMG opened" );
241 /* Fill p_demux field */
242 p_demux->pf_demux = Demux;
243 p_demux->pf_control = Control;
244 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
245 memset( p_sys, 0, sizeof( demux_sys_t ) );
247 ps_track_init( p_sys->tk );
248 p_sys->i_aspect = -1;
249 p_sys->i_mux_rate = 0;
251 p_sys->p_dvdread = p_dvdread;
252 p_sys->p_vmg_file = p_vmg_file;
253 p_sys->p_title = NULL;
254 p_sys->p_vts_file = NULL;
256 p_sys->i_title = p_sys->i_chapter = -1;
258 var_Create( p_demux, "dvdread-angle", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
259 var_Get( p_demux, "dvdread-angle", &val );
260 p_sys->i_angle = val.i_int > 0 ? val.i_int : 1;
262 DemuxTitles( p_demux, &p_sys->i_angle );
263 DvdReadSetArea( p_demux, 0, 0, p_sys->i_angle );
265 /* Update default_pts to a suitable value for dvdread access */
266 var_Create( p_demux, "dvdread-caching",
267 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
272 /*****************************************************************************
274 *****************************************************************************/
275 static void Close( vlc_object_t *p_this )
277 demux_t *p_demux = (demux_t*)p_this;
278 demux_sys_t *p_sys = p_demux->p_sys;
281 for( i = 0; i < PS_TK_COUNT; i++ )
283 ps_track_t *tk = &p_sys->tk[i];
286 es_format_Clean( &tk->fmt );
287 if( tk->es ) es_out_Del( p_demux->out, tk->es );
291 /* Close libdvdread */
292 if( p_sys->p_title ) DVDCloseFile( p_sys->p_title );
293 if( p_sys->p_vts_file ) ifoClose( p_sys->p_vts_file );
294 if( p_sys->p_vmg_file ) ifoClose( p_sys->p_vmg_file );
295 DVDClose( p_sys->p_dvdread );
300 /*****************************************************************************
302 *****************************************************************************/
303 static int Control( demux_t *p_demux, int i_query, va_list args )
305 demux_sys_t *p_sys = p_demux->p_sys;
309 input_title_t ***ppp_title;
315 case DEMUX_GET_POSITION:
317 pf = (double*) va_arg( args, double* );
319 if( p_sys->i_title_blocks > 0 )
320 *pf = (double)p_sys->i_title_offset / p_sys->i_title_blocks;
326 case DEMUX_SET_POSITION:
328 f = (double)va_arg( args, double );
330 DvdReadSeek( p_demux, f * p_sys->i_title_blocks );
335 pi64 = (int64_t*)va_arg( args, int64_t * );
336 if( p_sys->i_mux_rate > 0 )
338 *pi64 = (int64_t)1000000 * DVD_VIDEO_LB_LEN *
339 p_sys->i_title_offset / 50 / p_sys->i_mux_rate;
345 case DEMUX_GET_LENGTH:
346 pi64 = (int64_t*)va_arg( args, int64_t * );
347 if( p_sys->i_mux_rate > 0 )
349 *pi64 = (int64_t)1000000 * DVD_VIDEO_LB_LEN *
350 p_sys->i_title_blocks / 50 / p_sys->i_mux_rate;
356 /* Special for access_demux */
357 case DEMUX_CAN_PAUSE:
358 case DEMUX_CAN_CONTROL_PACE:
360 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
364 case DEMUX_SET_PAUSE_STATE:
367 case DEMUX_GET_TITLE_INFO:
368 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
369 pi_int = (int*)va_arg( args, int* );
370 *((int*)va_arg( args, int* )) = 1; /* Title offset */
371 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
373 /* Duplicate title infos */
374 *pi_int = p_sys->i_titles;
375 *ppp_title = malloc( sizeof(input_title_t **) * p_sys->i_titles );
376 for( i = 0; i < p_sys->i_titles; i++ )
378 (*ppp_title)[i] = vlc_input_title_Duplicate(p_sys->titles[i]);
382 case DEMUX_SET_TITLE:
383 i = (int)va_arg( args, int );
384 if( DvdReadSetArea( p_demux, i, 0, -1 ) != VLC_SUCCESS )
386 msg_Warn( p_demux, "cannot set title/chapter" );
389 p_demux->info.i_update |=
390 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
391 p_demux->info.i_title = i;
392 p_demux->info.i_seekpoint = 0;
395 case DEMUX_SET_SEEKPOINT:
396 i = (int)va_arg( args, int );
397 if( DvdReadSetArea( p_demux, -1, i, -1 ) != VLC_SUCCESS )
399 msg_Warn( p_demux, "cannot set title/chapter" );
402 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
403 p_demux->info.i_seekpoint = i;
406 case DEMUX_GET_PTS_DELAY:
407 pi64 = (int64_t*)va_arg( args, int64_t * );
408 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdread-caching" )*1000;
411 /* TODO implement others */
417 /*****************************************************************************
419 *****************************************************************************/
420 static int Demux( demux_t *p_demux )
422 demux_sys_t *p_sys = p_demux->p_sys;
424 uint8_t p_buffer[DVD_VIDEO_LB_LEN * DVD_BLOCK_READ_ONCE];
425 int i_blocks_once, i_read;
429 * Playback by cell in this pgc, starting at the cell for our chapter.
433 * Check end of pack, and select the following one
435 if( !p_sys->i_pack_len )
437 /* Read NAV packet */
438 if( DVDReadBlocks( p_sys->p_title, p_sys->i_next_vobu,
441 msg_Err( p_demux, "read failed for block %d", p_sys->i_next_vobu );
445 /* Basic check to be sure we don't have a empty title
446 * go to next title if so */
447 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
449 /* Parse the contained dsi packet */
450 DvdReadHandleDSI( p_demux, p_buffer );
453 if( p_sys->i_next_vobu > p_sys->i_title_end_block )
455 if( p_sys->i_title + 1 >= p_sys->i_titles )
460 DvdReadSetArea( p_demux, p_sys->i_title + 1, 0, -1 );
463 if( p_sys->i_pack_len >= 1024 )
465 msg_Err( p_demux, "i_pack_len >= 1024 (%i). "
466 "This shouldn't happen!", p_sys->i_pack_len );
470 /* FIXME: Ugly kludge: we send the pack block to the input for it
471 * sometimes has a zero scr and restart the sync */
472 p_sys->i_cur_block++;
473 p_sys->i_title_offset++;
475 DemuxBlock( p_demux, p_buffer, DVD_VIDEO_LB_LEN );
478 if( p_sys->i_cur_block > p_sys->i_title_end_block )
480 if( p_sys->i_title + 1 >= p_sys->i_titles )
485 DvdReadSetArea( p_demux, p_sys->i_title + 1, 0, -1 );
491 i_blocks_once = __MIN( p_sys->i_pack_len, DVD_BLOCK_READ_ONCE );
492 p_sys->i_pack_len -= i_blocks_once;
495 i_read = DVDReadBlocks( p_sys->p_title, p_sys->i_cur_block,
496 i_blocks_once, p_buffer );
497 if( i_read != i_blocks_once )
499 msg_Err( p_demux, "read failed for %d/%d blocks at 0x%02x",
500 i_read, i_blocks_once, p_sys->i_cur_block );
504 p_sys->i_cur_block += i_read;
505 p_sys->i_title_offset += i_read;
508 msg_Dbg( p_demux, "i_blocks: %d len: %d current: 0x%02x",
509 i_read, p_sys->i_pack_len, p_sys->i_cur_block );
512 for( i = 0; i < i_read; i++ )
514 DemuxBlock( p_demux, p_buffer + i * DVD_VIDEO_LB_LEN,
523 /*****************************************************************************
524 * DemuxBlock: demux a given block
525 *****************************************************************************/
526 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
528 demux_sys_t *p_sys = p_demux->p_sys;
531 while( p < &pkt[i_pkt] )
533 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
541 p_pkt = block_New( p_demux, i_size );
542 memcpy( p_pkt->p_buffer, p, i_size);
544 /* Parse it and send it */
545 switch( 0x100 | p[3] )
554 msg_Warn( p_demux, "received a PSM packet" );
556 else if( p[3] == 0xbb )
558 msg_Warn( p_demux, "received a SYSTEM packet" );
561 block_Release( p_pkt );
568 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
570 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
571 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
573 block_Release( p_pkt );
578 int i_id = ps_pkt_id( p_pkt );
581 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
585 ESNew( p_demux, i_id, 0 );
587 if( tk->b_seen && tk->es &&
588 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
590 es_out_Send( p_demux->out, tk->es, p_pkt );
594 block_Release( p_pkt );
599 block_Release( p_pkt );
611 /*****************************************************************************
612 * ESNew: register a new elementary stream
613 *****************************************************************************/
614 static void ESNew( demux_t *p_demux, int i_id, int i_lang )
616 demux_sys_t *p_sys = p_demux->p_sys;
617 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
618 char psz_language[3];
620 if( tk->b_seen ) return;
622 if( ps_track_fill( tk, 0, i_id ) )
624 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
628 psz_language[0] = psz_language[1] = psz_language[2] = 0;
629 if( i_lang && i_lang != 0xffff )
631 psz_language[0] = (i_lang >> 8)&0xff;
632 psz_language[1] = (i_lang )&0xff;
636 if( tk->fmt.i_cat == VIDEO_ES )
638 if( p_sys->i_aspect >= 0 )
640 tk->fmt.video.i_aspect = p_sys->i_aspect;
643 else if( tk->fmt.i_cat == AUDIO_ES )
646 /* find the audio number PLEASE find another way */
647 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
651 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
655 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
659 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
664 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
666 else if( tk->fmt.i_cat == SPU_ES )
669 tk->fmt.subs.spu.palette[0] = 0xBeef;
670 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
671 16 * sizeof( uint32_t ) );
673 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
676 tk->es = es_out_Add( p_demux->out, &tk->fmt );
677 tk->b_seen = VLC_TRUE;
680 /*****************************************************************************
681 * DvdReadSetArea: initialize input data for title x, chapter y.
682 * It should be called for each user navigation request.
683 *****************************************************************************
684 * Take care that i_title and i_chapter start from 0.
685 *****************************************************************************/
686 static int DvdReadSetArea( demux_t *p_demux, int i_title, int i_chapter,
689 demux_sys_t *p_sys = p_demux->p_sys;
690 int pgc_id = 0, pgn = 0;
693 #define p_pgc p_sys->p_cur_pgc
694 #define p_vmg p_sys->p_vmg_file
695 #define p_vts p_sys->p_vts_file
697 if( i_title >= 0 && i_title < p_sys->i_titles &&
698 i_title != p_sys->i_title )
700 int i_start_cell, i_end_cell;
702 if( p_sys->p_title != NULL ) DVDCloseFile( p_sys->p_title );
703 if( p_vts != NULL ) ifoClose( p_vts );
704 p_sys->i_title = i_title;
707 * We have to load all title information
709 msg_Dbg( p_demux, "open VTS %d, for title %d",
710 p_vmg->tt_srpt->title[i_title].title_set_nr, i_title + 1 );
713 if( !( p_vts = ifoOpen( p_sys->p_dvdread,
714 p_vmg->tt_srpt->title[i_title].title_set_nr ) ) )
716 msg_Err( p_demux, "fatal error in vts ifo" );
720 /* Title position inside the selected vts */
721 p_sys->i_ttn = p_vmg->tt_srpt->title[i_title].vts_ttn;
723 /* Find title start/end */
724 pgc_id = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgcn;
725 pgn = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgn;
726 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
728 p_sys->i_title_start_cell =
729 i_start_cell = p_pgc->program_map[pgn - 1] - 1;
730 p_sys->i_title_start_block =
731 p_pgc->cell_playback[i_start_cell].first_sector;
733 p_sys->i_title_end_cell =
734 i_end_cell = p_pgc->nr_of_cells - 1;
735 p_sys->i_title_end_block =
736 p_pgc->cell_playback[i_end_cell].last_sector;
738 p_sys->i_title_offset = 0;
740 p_sys->i_title_blocks = 0;
741 for( i = i_start_cell; i <= i_end_cell; i++ )
743 p_sys->i_title_blocks += p_pgc->cell_playback[i].last_sector -
744 p_pgc->cell_playback[i].first_sector + 1;
747 msg_Dbg( p_demux, "title %d vts_title %d pgc %d pgn %d "
748 "start %d end %d blocks: %d",
749 i_title + 1, p_sys->i_ttn, pgc_id, pgn,
750 p_sys->i_title_start_block, p_sys->i_title_end_block,
751 p_sys->i_title_blocks );
754 * Set properties for current chapter
756 p_sys->i_chapter = 0;
758 p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].nr_of_ptts;
760 pgc_id = p_vts->vts_ptt_srpt->title[
761 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgcn;
762 pgn = p_vts->vts_ptt_srpt->title[
763 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgn;
765 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
766 p_sys->i_pack_len = 0;
768 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
769 DvdReadFindCell( p_demux );
771 p_sys->i_next_vobu = p_sys->i_cur_block =
772 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
777 p_sys->i_angles = p_vmg->tt_srpt->title[i_title].nr_of_angles;
778 if( p_sys->i_angle > p_sys->i_angles ) p_sys->i_angle = 1;
781 * We've got enough info, time to open the title set data.
783 if( !( p_sys->p_title = DVDOpenFile( p_sys->p_dvdread,
784 p_vmg->tt_srpt->title[i_title].title_set_nr,
785 DVD_READ_TITLE_VOBS ) ) )
787 msg_Err( p_demux, "cannot open title (VTS_%02d_1.VOB)",
788 p_vmg->tt_srpt->title[i_title].title_set_nr );
792 //IfoPrintTitle( p_demux );
795 * Destroy obsolete ES by reinitializing program 0
796 * and find all ES in title with ifo data
798 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
800 for( i = 0; i < PS_TK_COUNT; i++ )
802 ps_track_t *tk = &p_sys->tk[i];
805 es_format_Clean( &tk->fmt );
806 if( tk->es ) es_out_Del( p_demux->out, tk->es );
808 tk->b_seen = VLC_FALSE;
811 if( p_demux->info.i_title != i_title )
813 p_demux->info.i_update |=
814 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
815 p_demux->info.i_title = i_title;
816 p_demux->info.i_seekpoint = 0;
819 /* TODO: re-add angles */
822 ESNew( p_demux, 0xe0, 0 ); /* Video, FIXME ? */
823 p_sys->i_aspect = p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio;
825 #define audio_control \
826 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
828 /* Audio ES, in the order they appear in the .ifo */
829 for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
834 //IfoPrintAudio( p_demux, i );
836 /* Audio channel is active if first byte is 0x80 */
837 if( audio_control & 0x8000 )
839 i_position = ( audio_control & 0x7F00 ) >> 8;
841 msg_Dbg( p_demux, "audio position %d", i_position );
842 switch( p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format )
845 i_id = (0x80 + i_position) | 0xbd00;
848 case 0x03: /* MPEG audio */
849 i_id = 0xc000 + i_position;
851 case 0x04: /* LPCM */
852 i_id = (0xa0 + i_position) | 0xbd00;
855 i_id = (0x88 + i_position) | 0xbd00;
859 msg_Err( p_demux, "unknown audio type %.2x",
860 p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format );
863 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
864 vts_audio_attr[i - 1].lang_code );
869 #define spu_palette \
870 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette
872 memcpy( p_sys->clut, spu_palette, 16 * sizeof( uint32_t ) );
874 #define spu_control \
875 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
878 for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
883 //IfoPrintSpu( p_sys, i );
884 msg_Dbg( p_demux, "spu %d 0x%02x", i, spu_control );
886 if( spu_control & 0x80000000 )
888 /* there are several streams for one spu */
889 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
892 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
894 case 1: /* letterbox */
895 i_position = spu_control & 0xff;
897 case 2: /* pan&scan */
898 i_position = ( spu_control >> 8 ) & 0xff;
900 default: /* widescreen */
901 i_position = ( spu_control >> 16 ) & 0xff;
908 i_position = ( spu_control >> 24 ) & 0x7F;
911 i_id = (0x20 + i_position) | 0xbd00;
913 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
914 vts_subp_attr[i - 1].lang_code );
920 else if( i_title != -1 && i_title != p_sys->i_title )
923 return VLC_EGENERIC; /* Couldn't set title */
930 if( i_chapter >= 0 && i_chapter < p_sys->i_chapters )
932 pgc_id = p_vts->vts_ptt_srpt->title[
933 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
934 pgn = p_vts->vts_ptt_srpt->title[
935 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
937 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
939 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
940 p_sys->i_chapter = i_chapter;
941 DvdReadFindCell( p_demux );
943 p_sys->i_title_offset = 0;
944 for( i = p_sys->i_title_start_cell; i < p_sys->i_cur_cell; i++ )
946 p_sys->i_title_offset += p_pgc->cell_playback[i].last_sector -
947 p_pgc->cell_playback[i].first_sector + 1;
950 p_sys->i_pack_len = 0;
951 p_sys->i_next_vobu = p_sys->i_cur_block =
952 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
954 if( p_demux->info.i_seekpoint != i_chapter )
956 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
957 p_demux->info.i_seekpoint = i_chapter;
960 else if( i_chapter != -1 )
963 return VLC_EGENERIC; /* Couldn't set chapter */
973 /*****************************************************************************
974 * DvdReadSeek : Goes to a given position on the stream.
975 *****************************************************************************
976 * This one is used by the input and translate chronological position from
977 * input to logical position on the device.
978 *****************************************************************************/
979 static void DvdReadSeek( demux_t *p_demux, int i_block_offset )
981 demux_sys_t *p_sys = p_demux->p_sys;
988 #define p_pgc p_sys->p_cur_pgc
989 #define p_vts p_sys->p_vts_file
992 i_block = i_block_offset;
993 for( i_cell = p_sys->i_title_start_cell;
994 i_cell <= p_sys->i_title_end_cell; i_cell++ )
996 if( i_block < (int)p_pgc->cell_playback[i_cell].last_sector -
997 (int)p_pgc->cell_playback[i_cell].first_sector + 1 ) break;
999 i_block -= (p_pgc->cell_playback[i_cell].last_sector -
1000 p_pgc->cell_playback[i_cell].first_sector + 1);
1002 if( i_cell > p_sys->i_title_end_cell )
1004 msg_Err( p_demux, "couldn't find cell for block %i", i_block_offset );
1007 i_block += p_pgc->cell_playback[i_cell].first_sector;
1008 p_sys->i_title_offset = i_block_offset;
1011 for( i_chapter = 0; i_chapter < p_sys->i_chapters; i_chapter++ )
1013 int pgc_id, pgn, i_tmp;
1015 pgc_id = p_vts->vts_ptt_srpt->title[
1016 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
1017 pgn = p_vts->vts_ptt_srpt->title[
1018 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
1020 i_tmp = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc->program_map[pgn-1];
1022 if( i_tmp > i_cell ) break;
1025 if( i_chapter < p_sys->i_chapters &&
1026 p_demux->info.i_seekpoint != i_chapter )
1028 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1029 p_demux->info.i_seekpoint = i_chapter;
1033 while( (int)p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu] <= i_block )
1039 while( p_vts->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1040 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1046 msg_Dbg( p_demux, "cell %d i_sub_cell %d chapter %d vobu %d "
1047 "cell_sector %d vobu_sector %d sub_cell_sector %d",
1048 i_cell, i_sub_cell, i_chapter, i_vobu,
1049 p_sys->p_cur_pgc->cell_playback[i_cell].first_sector,
1050 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu],
1051 p_vts->vts_c_adt->cell_adr_table[i_sub_cell - 1].start_sector);
1054 p_sys->i_cur_block = i_block;
1055 p_sys->i_next_vobu = p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu];
1056 p_sys->i_pack_len = p_sys->i_next_vobu - i_block;
1057 p_sys->i_cur_cell = i_cell;
1058 p_sys->i_chapter = i_chapter;
1059 DvdReadFindCell( p_demux );
1067 /*****************************************************************************
1069 *****************************************************************************/
1070 static void DvdReadHandleDSI( demux_t *p_demux, uint8_t *p_data )
1072 demux_sys_t *p_sys = p_demux->p_sys;
1074 navRead_DSI( &p_sys->dsi_pack, &p_data[DSI_START_BYTE] );
1077 * Determine where we go next. These values are the ones we mostly
1080 p_sys->i_cur_block = p_sys->dsi_pack.dsi_gi.nv_pck_lbn;
1081 p_sys->i_pack_len = p_sys->dsi_pack.dsi_gi.vobu_ea;
1084 * If we're not at the end of this cell, we can determine the next
1085 * VOBU to display using the VOBU_SRI information section of the
1086 * DSI. Using this value correctly follows the current angle,
1087 * avoiding the doubled scenes in The Matrix, and makes our life
1091 p_sys->i_next_vobu = p_sys->i_cur_block +
1092 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1094 if( p_sys->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL
1095 && p_sys->i_angle > 1 )
1097 switch( ( p_sys->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1100 /* Interleaved unit with no angle */
1101 if( p_sys->dsi_pack.sml_pbi.ilvu_sa != 0 )
1103 p_sys->i_next_vobu = p_sys->i_cur_block +
1104 p_sys->dsi_pack.sml_pbi.ilvu_sa;
1105 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1109 p_sys->i_next_vobu = p_sys->i_cur_block +
1110 p_sys->dsi_pack.dsi_gi.vobu_ea + 1;
1114 /* vobu is end of ilvu */
1115 if( p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address )
1117 p_sys->i_next_vobu = p_sys->i_cur_block +
1118 p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address;
1119 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1124 /* vobu is beginning of ilvu */
1128 /* entering interleaved section */
1130 /* non interleaved cells in interleaved section */
1132 p_sys->i_next_vobu = p_sys->i_cur_block +
1133 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1137 else if( p_sys->dsi_pack.vobu_sri.next_vobu == SRI_END_OF_CELL )
1139 p_sys->i_cur_cell = p_sys->i_next_cell;
1140 DvdReadFindCell( p_demux );
1142 p_sys->i_next_vobu =
1143 p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1147 msg_Dbg( p_demux, 12, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d",
1148 p_sys->dsi_pack.dsi_gi.nv_pck_scr,
1149 p_sys->dsi_pack.dsi_gi.nv_pck_lbn,
1150 p_sys->dsi_pack.dsi_gi.vobu_ea,
1151 p_sys->dsi_pack.dsi_gi.vobu_vob_idn,
1152 p_sys->dsi_pack.dsi_gi.vobu_c_idn );
1154 msg_Dbg( p_demux, 12, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1155 p_sys->dsi_pack.sml_pbi.category,
1156 p_sys->dsi_pack.sml_pbi.ilvu_ea,
1157 p_sys->dsi_pack.sml_pbi.ilvu_sa,
1158 p_sys->dsi_pack.sml_pbi.size );
1160 msg_Dbg( p_demux, 12, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1161 p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1162 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle - 1 ].address,
1163 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle ].address);
1167 /*****************************************************************************
1169 *****************************************************************************/
1170 static void DvdReadFindCell( demux_t *p_demux )
1172 demux_sys_t *p_sys = p_demux->p_sys;
1178 #define cell p_sys->p_cur_pgc->cell_playback
1180 if( cell[p_sys->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1182 p_sys->i_cur_cell += p_sys->i_angle - 1;
1184 while( cell[p_sys->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1188 p_sys->i_next_cell = p_sys->i_cur_cell + i + 1;
1192 p_sys->i_next_cell = p_sys->i_cur_cell + 1;
1197 if( p_sys->i_chapter + 1 >= p_sys->i_chapters ) return;
1199 pgc_id = p_sys->p_vts_file->vts_ptt_srpt->title[
1200 p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgcn;
1201 pgn = p_sys->p_vts_file->vts_ptt_srpt->title[
1202 p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgn;
1203 p_pgc = p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
1205 if( p_sys->i_cur_cell >= p_pgc->program_map[pgn - 1] - 1 )
1209 if( p_sys->i_chapter < p_sys->i_chapters &&
1210 p_demux->info.i_seekpoint != p_sys->i_chapter )
1212 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1213 p_demux->info.i_seekpoint = p_sys->i_chapter;
1218 /*****************************************************************************
1219 * DemuxTitles: get the titles/chapters structure
1220 *****************************************************************************/
1221 static void DemuxTitles( demux_t *p_demux, int *pi_angle )
1223 demux_sys_t *p_sys = p_demux->p_sys;
1229 /* Find out number of titles/chapters */
1230 #define tt_srpt p_sys->p_vmg_file->tt_srpt
1232 i_titles = tt_srpt->nr_of_srpts;
1233 msg_Dbg( p_demux, "number of titles: %d", i_titles );
1235 for( i = 0; i < i_titles; i++ )
1237 int32_t i_chapters = 0;
1240 i_chapters = tt_srpt->title[i].nr_of_ptts;
1241 msg_Dbg( p_demux, "title %d has %d chapters", i, i_chapters );
1243 t = vlc_input_title_New();
1245 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
1247 s = vlc_seekpoint_New();
1248 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1251 TAB_APPEND( p_sys->i_titles, p_sys->titles, t );