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_shortname( _("DVD without menus") );
91 set_description( _("DVDRead Input (DVD without menu support)") );
92 set_category( CAT_INPUT );
93 set_subcategory( SUBCAT_INPUT_ACCESS );
94 add_integer( "dvdread-angle", 1, NULL, ANGLE_TEXT,
95 ANGLE_LONGTEXT, VLC_FALSE );
96 add_integer( "dvdread-caching", DEFAULT_PTS_DELAY / 1000, NULL,
97 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
98 add_string( "dvdread-css-method", NULL, NULL, CSSMETHOD_TEXT,
99 CSSMETHOD_LONGTEXT, VLC_TRUE );
100 change_string_list( psz_css_list, psz_css_list_text, 0 );
101 set_capability( "access_demux", 0 );
102 add_shortcut( "dvd" );
103 add_shortcut( "dvdread" );
104 add_shortcut( "dvdsimple" );
105 set_callbacks( Open, Close );
108 /* how many blocks DVDRead will read in each loop */
109 #define DVD_BLOCK_READ_ONCE 4
111 /*****************************************************************************
113 *****************************************************************************/
118 dvd_reader_t *p_dvdread;
121 ifo_handle_t *p_vmg_file;
122 ifo_handle_t *p_vts_file;
125 int i_chapter, i_chapters;
126 int i_angle, i_angles;
128 tt_srpt_t *p_tt_srpt;
137 /* Current title start/end blocks */
138 int i_title_start_block;
139 int i_title_end_block;
142 mtime_t i_title_cur_time;
144 int i_title_start_cell;
145 int i_title_end_cell;
148 mtime_t i_cell_cur_time;
149 mtime_t i_cell_duration;
152 ps_track_t tk[PS_TK_COUNT];
155 input_title_t **titles;
164 static int Control ( demux_t *, int, va_list );
165 static int Demux ( demux_t * );
166 static int DemuxBlock( demux_t *, uint8_t *, int );
168 static void DemuxTitles( demux_t *, int * );
169 static void ESNew( demux_t *, int, int );
171 static int DvdReadSetArea ( demux_t *, int, int, int );
172 static void DvdReadSeek ( demux_t *, int );
173 static void DvdReadHandleDSI( demux_t *, uint8_t * );
174 static void DvdReadFindCell ( demux_t * );
176 /*****************************************************************************
178 *****************************************************************************/
179 static int Open( vlc_object_t *p_this )
181 demux_t *p_demux = (demux_t*)p_this;
184 char *psz_dvdcss_env;
185 dvd_reader_t *p_dvdread;
186 ifo_handle_t *p_vmg_file;
189 if( !p_demux->psz_path || !*p_demux->psz_path )
191 /* Only when selected */
192 if( !p_this->b_force ) return VLC_EGENERIC;
194 psz_name = var_CreateGetString( p_this, "dvd" );
195 if( !psz_name || !*psz_name )
197 if( psz_name ) free( psz_name );
201 else psz_name = strdup( p_demux->psz_path );
204 if( psz_name[0] && psz_name[1] == ':' &&
205 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
208 /* Override environment variable DVDCSS_METHOD with config option
209 * (FIXME: this creates a small memory leak) */
210 psz_dvdcss_env = config_GetPsz( p_demux, "dvdread-css-method" );
211 if( psz_dvdcss_env && *psz_dvdcss_env )
215 psz_env = malloc( strlen("DVDCSS_METHOD=") +
216 strlen( psz_dvdcss_env ) + 1 );
219 free( psz_dvdcss_env );
223 sprintf( psz_env, "%s%s", "DVDCSS_METHOD=", psz_dvdcss_env );
227 if( psz_dvdcss_env ) free( psz_dvdcss_env );
230 if( !(p_dvdread = DVDOpen( psz_name )) )
232 msg_Err( p_demux, "DVDRead cannot open source: %s", psz_name );
238 /* Ifo allocation & initialisation */
239 if( !( p_vmg_file = ifoOpen( p_dvdread, 0 ) ) )
241 msg_Warn( p_demux, "cannot open VMG info" );
244 msg_Dbg( p_demux, "VMG opened" );
246 /* Fill p_demux field */
247 p_demux->pf_demux = Demux;
248 p_demux->pf_control = Control;
249 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
250 memset( p_sys, 0, sizeof( demux_sys_t ) );
252 ps_track_init( p_sys->tk );
253 p_sys->i_aspect = -1;
254 p_sys->i_title_cur_time = (mtime_t) 0;
255 p_sys->i_cell_cur_time = (mtime_t) 0;
256 p_sys->i_cell_duration = (mtime_t) 0;
258 p_sys->p_dvdread = p_dvdread;
259 p_sys->p_vmg_file = p_vmg_file;
260 p_sys->p_title = NULL;
261 p_sys->p_vts_file = NULL;
263 p_sys->i_title = p_sys->i_chapter = -1;
265 var_Create( p_demux, "dvdread-angle", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
266 var_Get( p_demux, "dvdread-angle", &val );
267 p_sys->i_angle = val.i_int > 0 ? val.i_int : 1;
269 DemuxTitles( p_demux, &p_sys->i_angle );
270 if( DvdReadSetArea( p_demux, 0, 0, p_sys->i_angle ) != VLC_SUCCESS )
273 msg_Err( p_demux, "DvdReadSetArea(0,0,%i) failed (can't decrypt DVD?)",
278 /* Update default_pts to a suitable value for dvdread access */
279 var_Create( p_demux, "dvdread-caching",
280 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
285 /*****************************************************************************
287 *****************************************************************************/
288 static void Close( vlc_object_t *p_this )
290 demux_t *p_demux = (demux_t*)p_this;
291 demux_sys_t *p_sys = p_demux->p_sys;
294 for( i = 0; i < PS_TK_COUNT; i++ )
296 ps_track_t *tk = &p_sys->tk[i];
299 es_format_Clean( &tk->fmt );
300 if( tk->es ) es_out_Del( p_demux->out, tk->es );
304 /* Close libdvdread */
305 if( p_sys->p_title ) DVDCloseFile( p_sys->p_title );
306 if( p_sys->p_vts_file ) ifoClose( p_sys->p_vts_file );
307 if( p_sys->p_vmg_file ) ifoClose( p_sys->p_vmg_file );
308 DVDClose( p_sys->p_dvdread );
313 static int64_t dvdtime_to_time( dvd_time_t *dtime, uint8_t still_time )
315 /* Macro to convert Binary Coded Decimal to Decimal */
316 #define BCD2D(__x__) (((__x__ & 0xf0) >> 4) * 10 + (__x__ & 0x0f))
319 int64_t i_micro_second = 0;
321 if (still_time == 0 || still_time == 0xFF)
323 i_micro_second += (int64_t)(BCD2D(dtime->hour)) * 60 * 60 * 1000000;
324 i_micro_second += (int64_t)(BCD2D(dtime->minute)) * 60 * 1000000;
325 i_micro_second += (int64_t)(BCD2D(dtime->second)) * 1000000;
327 switch((dtime->frame_u & 0xc0) >> 6)
339 f_ms = BCD2D(dtime->frame_u&0x3f) * 1000.0 / f_fps;
340 i_micro_second += (int64_t)(f_ms * 1000.0);
344 i_micro_second = still_time;
345 i_micro_second = (int64_t)((double)i_micro_second * 1000000.0);
348 return i_micro_second;
351 /*****************************************************************************
353 *****************************************************************************/
354 static int Control( demux_t *p_demux, int i_query, va_list args )
356 demux_sys_t *p_sys = p_demux->p_sys;
360 input_title_t ***ppp_title;
366 case DEMUX_GET_POSITION:
368 pf = (double*) va_arg( args, double* );
370 if( p_sys->i_title_blocks > 0 )
371 *pf = (double)p_sys->i_title_offset / p_sys->i_title_blocks;
377 case DEMUX_SET_POSITION:
379 f = (double)va_arg( args, double );
381 DvdReadSeek( p_demux, f * p_sys->i_title_blocks );
386 pi64 = (int64_t*)va_arg( args, int64_t * );
387 if( p_sys->i_title_cur_time > 0 )
389 *pi64 = (int64_t)p_sys->i_title_cur_time;
395 case DEMUX_GET_LENGTH:
396 pi64 = (int64_t*)va_arg( args, int64_t * );
397 if (p_demux->info.i_title >= 0 && p_demux->info.i_title < p_sys->i_titles)
399 *pi64 = dvdtime_to_time( &p_sys->p_cur_pgc->playback_time, 0 );
405 /* Special for access_demux */
406 case DEMUX_CAN_PAUSE:
407 case DEMUX_CAN_CONTROL_PACE:
409 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
413 case DEMUX_SET_PAUSE_STATE:
416 case DEMUX_GET_TITLE_INFO:
417 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
418 pi_int = (int*)va_arg( args, int* );
419 *((int*)va_arg( args, int* )) = 1; /* Title offset */
420 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
422 /* Duplicate title infos */
423 *pi_int = p_sys->i_titles;
424 *ppp_title = malloc( sizeof(input_title_t **) * p_sys->i_titles );
425 for( i = 0; i < p_sys->i_titles; i++ )
427 (*ppp_title)[i] = vlc_input_title_Duplicate(p_sys->titles[i]);
431 case DEMUX_SET_TITLE:
432 i = (int)va_arg( args, int );
433 if( DvdReadSetArea( p_demux, i, 0, -1 ) != VLC_SUCCESS )
435 msg_Warn( p_demux, "cannot set title/chapter" );
438 p_demux->info.i_update |=
439 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
440 p_demux->info.i_title = i;
441 p_demux->info.i_seekpoint = 0;
444 case DEMUX_SET_SEEKPOINT:
445 i = (int)va_arg( args, int );
446 if( DvdReadSetArea( p_demux, -1, i, -1 ) != VLC_SUCCESS )
448 msg_Warn( p_demux, "cannot set title/chapter" );
451 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
452 p_demux->info.i_seekpoint = i;
455 case DEMUX_GET_PTS_DELAY:
456 pi64 = (int64_t*)va_arg( args, int64_t * );
457 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdread-caching" )*1000;
460 /* TODO implement others */
466 /*****************************************************************************
468 *****************************************************************************/
469 static int Demux( demux_t *p_demux )
471 demux_sys_t *p_sys = p_demux->p_sys;
473 uint8_t p_buffer[DVD_VIDEO_LB_LEN * DVD_BLOCK_READ_ONCE];
474 int i_blocks_once, i_read;
478 * Playback by cell in this pgc, starting at the cell for our chapter.
482 * Check end of pack, and select the following one
484 if( !p_sys->i_pack_len )
486 /* Read NAV packet */
487 if( DVDReadBlocks( p_sys->p_title, p_sys->i_next_vobu,
490 msg_Err( p_demux, "read failed for block %d", p_sys->i_next_vobu );
494 /* Basic check to be sure we don't have a empty title
495 * go to next title if so */
496 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
498 /* Parse the contained dsi packet */
499 DvdReadHandleDSI( p_demux, p_buffer );
502 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells )
504 if( p_sys->i_title + 1 >= p_sys->i_titles )
509 DvdReadSetArea( p_demux, p_sys->i_title + 1, 0, -1 );
512 if( p_sys->i_pack_len >= 1024 )
514 msg_Err( p_demux, "i_pack_len >= 1024 (%i). "
515 "This shouldn't happen!", p_sys->i_pack_len );
519 /* FIXME: Ugly kludge: we send the pack block to the input for it
520 * sometimes has a zero scr and restart the sync */
521 p_sys->i_cur_block++;
522 p_sys->i_title_offset++;
524 DemuxBlock( p_demux, p_buffer, DVD_VIDEO_LB_LEN );
527 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells )
529 if( p_sys->i_title + 1 >= p_sys->i_titles )
534 DvdReadSetArea( p_demux, p_sys->i_title + 1, 0, -1 );
540 i_blocks_once = __MIN( p_sys->i_pack_len, DVD_BLOCK_READ_ONCE );
541 p_sys->i_pack_len -= i_blocks_once;
544 i_read = DVDReadBlocks( p_sys->p_title, p_sys->i_cur_block,
545 i_blocks_once, p_buffer );
546 if( i_read != i_blocks_once )
548 msg_Err( p_demux, "read failed for %d/%d blocks at 0x%02x",
549 i_read, i_blocks_once, p_sys->i_cur_block );
553 p_sys->i_cur_block += i_read;
554 p_sys->i_title_offset += i_read;
557 msg_Dbg( p_demux, "i_blocks: %d len: %d current: 0x%02x",
558 i_read, p_sys->i_pack_len, p_sys->i_cur_block );
561 for( i = 0; i < i_read; i++ )
563 DemuxBlock( p_demux, p_buffer + i * DVD_VIDEO_LB_LEN,
572 /*****************************************************************************
573 * DemuxBlock: demux a given block
574 *****************************************************************************/
575 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
577 demux_sys_t *p_sys = p_demux->p_sys;
580 while( p < &pkt[i_pkt] )
582 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
590 p_pkt = block_New( p_demux, i_size );
591 memcpy( p_pkt->p_buffer, p, i_size);
593 /* Parse it and send it */
594 switch( 0x100 | p[3] )
603 msg_Warn( p_demux, "received a PSM packet" );
605 else if( p[3] == 0xbb )
607 msg_Warn( p_demux, "received a SYSTEM packet" );
610 block_Release( p_pkt );
617 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
619 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
621 block_Release( p_pkt );
626 int i_id = ps_pkt_id( p_pkt );
629 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
633 ESNew( p_demux, i_id, 0 );
635 if( tk->b_seen && tk->es &&
636 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
638 es_out_Send( p_demux->out, tk->es, p_pkt );
642 block_Release( p_pkt );
647 block_Release( p_pkt );
659 /*****************************************************************************
660 * ESNew: register a new elementary stream
661 *****************************************************************************/
662 static void ESNew( demux_t *p_demux, int i_id, int i_lang )
664 demux_sys_t *p_sys = p_demux->p_sys;
665 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
666 char psz_language[3];
668 if( tk->b_seen ) return;
670 if( ps_track_fill( tk, 0, i_id ) )
672 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
676 psz_language[0] = psz_language[1] = psz_language[2] = 0;
677 if( i_lang && i_lang != 0xffff )
679 psz_language[0] = (i_lang >> 8)&0xff;
680 psz_language[1] = (i_lang )&0xff;
684 if( tk->fmt.i_cat == VIDEO_ES )
686 if( p_sys->i_aspect >= 0 )
688 tk->fmt.video.i_aspect = p_sys->i_aspect;
691 else if( tk->fmt.i_cat == AUDIO_ES )
694 /* find the audio number PLEASE find another way */
695 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
699 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
703 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
707 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
712 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
714 else if( tk->fmt.i_cat == SPU_ES )
717 tk->fmt.subs.spu.palette[0] = 0xBeef;
718 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
719 16 * sizeof( uint32_t ) );
721 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
724 tk->es = es_out_Add( p_demux->out, &tk->fmt );
725 tk->b_seen = VLC_TRUE;
728 /*****************************************************************************
729 * DvdReadSetArea: initialize input data for title x, chapter y.
730 * It should be called for each user navigation request.
731 *****************************************************************************
732 * Take care that i_title and i_chapter start from 0.
733 *****************************************************************************/
734 static int DvdReadSetArea( demux_t *p_demux, int i_title, int i_chapter,
737 demux_sys_t *p_sys = p_demux->p_sys;
738 int pgc_id = 0, pgn = 0;
741 #define p_pgc p_sys->p_cur_pgc
742 #define p_vmg p_sys->p_vmg_file
743 #define p_vts p_sys->p_vts_file
745 if( i_title >= 0 && i_title < p_sys->i_titles &&
746 i_title != p_sys->i_title )
748 int i_start_cell, i_end_cell;
750 if( p_sys->p_title != NULL ) DVDCloseFile( p_sys->p_title );
751 if( p_vts != NULL ) ifoClose( p_vts );
752 p_sys->i_title = i_title;
755 * We have to load all title information
757 msg_Dbg( p_demux, "open VTS %d, for title %d",
758 p_vmg->tt_srpt->title[i_title].title_set_nr, i_title + 1 );
761 if( !( p_vts = ifoOpen( p_sys->p_dvdread,
762 p_vmg->tt_srpt->title[i_title].title_set_nr ) ) )
764 msg_Err( p_demux, "fatal error in vts ifo" );
768 /* Title position inside the selected vts */
769 p_sys->i_ttn = p_vmg->tt_srpt->title[i_title].vts_ttn;
771 /* Find title start/end */
772 pgc_id = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgcn;
773 pgn = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgn;
774 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
776 p_sys->i_title_start_cell =
777 i_start_cell = p_pgc->program_map[pgn - 1] - 1;
778 p_sys->i_title_start_block =
779 p_pgc->cell_playback[i_start_cell].first_sector;
781 p_sys->i_title_end_cell =
782 i_end_cell = p_pgc->nr_of_cells - 1;
783 p_sys->i_title_end_block =
784 p_pgc->cell_playback[i_end_cell].last_sector;
786 p_sys->i_title_offset = 0;
788 p_sys->i_title_blocks = 0;
789 for( i = i_start_cell; i <= i_end_cell; i++ )
791 p_sys->i_title_blocks += p_pgc->cell_playback[i].last_sector -
792 p_pgc->cell_playback[i].first_sector + 1;
795 msg_Dbg( p_demux, "title %d vts_title %d pgc %d pgn %d "
796 "start %d end %d blocks: %d",
797 i_title + 1, p_sys->i_ttn, pgc_id, pgn,
798 p_sys->i_title_start_block, p_sys->i_title_end_block,
799 p_sys->i_title_blocks );
802 * Set properties for current chapter
804 p_sys->i_chapter = 0;
806 p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].nr_of_ptts;
808 pgc_id = p_vts->vts_ptt_srpt->title[
809 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgcn;
810 pgn = p_vts->vts_ptt_srpt->title[
811 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgn;
813 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
814 p_sys->i_pack_len = 0;
816 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
817 DvdReadFindCell( p_demux );
819 p_sys->i_next_vobu = p_sys->i_cur_block =
820 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
825 p_sys->i_angles = p_vmg->tt_srpt->title[i_title].nr_of_angles;
826 if( p_sys->i_angle > p_sys->i_angles ) p_sys->i_angle = 1;
829 * We've got enough info, time to open the title set data.
831 if( !( p_sys->p_title = DVDOpenFile( p_sys->p_dvdread,
832 p_vmg->tt_srpt->title[i_title].title_set_nr,
833 DVD_READ_TITLE_VOBS ) ) )
835 msg_Err( p_demux, "cannot open title (VTS_%02d_1.VOB)",
836 p_vmg->tt_srpt->title[i_title].title_set_nr );
840 //IfoPrintTitle( p_demux );
843 * Destroy obsolete ES by reinitializing program 0
844 * and find all ES in title with ifo data
846 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
848 for( i = 0; i < PS_TK_COUNT; i++ )
850 ps_track_t *tk = &p_sys->tk[i];
853 es_format_Clean( &tk->fmt );
854 if( tk->es ) es_out_Del( p_demux->out, tk->es );
856 tk->b_seen = VLC_FALSE;
859 if( p_demux->info.i_title != i_title )
861 p_demux->info.i_update |=
862 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
863 p_demux->info.i_title = i_title;
864 p_demux->info.i_seekpoint = 0;
867 /* TODO: re-add angles */
870 ESNew( p_demux, 0xe0, 0 ); /* Video, FIXME ? */
871 p_sys->i_aspect = p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio;
873 #define audio_control \
874 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
876 /* Audio ES, in the order they appear in the .ifo */
877 for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
882 //IfoPrintAudio( p_demux, i );
884 /* Audio channel is active if first byte is 0x80 */
885 if( audio_control & 0x8000 )
887 i_position = ( audio_control & 0x7F00 ) >> 8;
889 msg_Dbg( p_demux, "audio position %d", i_position );
890 switch( p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format )
893 i_id = (0x80 + i_position) | 0xbd00;
896 case 0x03: /* MPEG audio */
897 i_id = 0xc000 + i_position;
899 case 0x04: /* LPCM */
900 i_id = (0xa0 + i_position) | 0xbd00;
903 i_id = (0x88 + i_position) | 0xbd00;
907 msg_Err( p_demux, "unknown audio type %.2x",
908 p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format );
911 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
912 vts_audio_attr[i - 1].lang_code );
917 #define spu_palette \
918 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette
920 memcpy( p_sys->clut, spu_palette, 16 * sizeof( uint32_t ) );
922 #define spu_control \
923 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
926 for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
931 //IfoPrintSpu( p_sys, i );
932 msg_Dbg( p_demux, "spu %d 0x%02x", i, spu_control );
934 if( spu_control & 0x80000000 )
936 /* there are several streams for one spu */
937 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
940 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
942 case 1: /* letterbox */
943 i_position = spu_control & 0xff;
945 case 2: /* pan&scan */
946 i_position = ( spu_control >> 8 ) & 0xff;
948 default: /* widescreen */
949 i_position = ( spu_control >> 16 ) & 0xff;
956 i_position = ( spu_control >> 24 ) & 0x7F;
959 i_id = (0x20 + i_position) | 0xbd00;
961 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
962 vts_subp_attr[i - 1].lang_code );
968 else if( i_title != -1 && i_title != p_sys->i_title )
971 return VLC_EGENERIC; /* Couldn't set title */
978 if( i_chapter >= 0 && i_chapter < p_sys->i_chapters )
980 pgc_id = p_vts->vts_ptt_srpt->title[
981 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
982 pgn = p_vts->vts_ptt_srpt->title[
983 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
985 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
987 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
988 p_sys->i_chapter = i_chapter;
989 DvdReadFindCell( p_demux );
991 p_sys->i_title_offset = 0;
992 for( i = p_sys->i_title_start_cell; i < p_sys->i_cur_cell; i++ )
994 p_sys->i_title_offset += p_pgc->cell_playback[i].last_sector -
995 p_pgc->cell_playback[i].first_sector + 1;
998 p_sys->i_pack_len = 0;
999 p_sys->i_next_vobu = p_sys->i_cur_block =
1000 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1002 if( p_demux->info.i_seekpoint != i_chapter )
1004 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1005 p_demux->info.i_seekpoint = i_chapter;
1008 else if( i_chapter != -1 )
1011 return VLC_EGENERIC; /* Couldn't set chapter */
1021 /*****************************************************************************
1022 * DvdReadSeek : Goes to a given position on the stream.
1023 *****************************************************************************
1024 * This one is used by the input and translate chronological position from
1025 * input to logical position on the device.
1026 *****************************************************************************/
1027 static void DvdReadSeek( demux_t *p_demux, int i_block_offset )
1029 demux_sys_t *p_sys = p_demux->p_sys;
1036 #define p_pgc p_sys->p_cur_pgc
1037 #define p_vts p_sys->p_vts_file
1040 i_block = i_block_offset;
1041 for( i_cell = p_sys->i_title_start_cell;
1042 i_cell <= p_sys->i_title_end_cell; i_cell++ )
1044 if( i_block < (int)p_pgc->cell_playback[i_cell].last_sector -
1045 (int)p_pgc->cell_playback[i_cell].first_sector + 1 ) break;
1047 i_block -= (p_pgc->cell_playback[i_cell].last_sector -
1048 p_pgc->cell_playback[i_cell].first_sector + 1);
1050 if( i_cell > p_sys->i_title_end_cell )
1052 msg_Err( p_demux, "couldn't find cell for block %i", i_block_offset );
1055 i_block += p_pgc->cell_playback[i_cell].first_sector;
1056 p_sys->i_title_offset = i_block_offset;
1059 for( i_chapter = 0; i_chapter < p_sys->i_chapters; i_chapter++ )
1061 int pgc_id, pgn, i_tmp;
1063 pgc_id = p_vts->vts_ptt_srpt->title[
1064 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
1065 pgn = p_vts->vts_ptt_srpt->title[
1066 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
1068 i_tmp = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc->program_map[pgn-1];
1070 if( i_tmp > i_cell ) break;
1073 if( i_chapter < p_sys->i_chapters &&
1074 p_demux->info.i_seekpoint != i_chapter )
1076 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1077 p_demux->info.i_seekpoint = i_chapter;
1081 while( (int)p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu] <= i_block )
1087 while( p_vts->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1088 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1094 msg_Dbg( p_demux, "cell %d i_sub_cell %d chapter %d vobu %d "
1095 "cell_sector %d vobu_sector %d sub_cell_sector %d",
1096 i_cell, i_sub_cell, i_chapter, i_vobu,
1097 p_sys->p_cur_pgc->cell_playback[i_cell].first_sector,
1098 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu],
1099 p_vts->vts_c_adt->cell_adr_table[i_sub_cell - 1].start_sector);
1102 p_sys->i_cur_block = i_block;
1103 p_sys->i_next_vobu = p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu];
1104 p_sys->i_pack_len = p_sys->i_next_vobu - i_block;
1105 p_sys->i_cur_cell = i_cell;
1106 p_sys->i_chapter = i_chapter;
1107 DvdReadFindCell( p_demux );
1115 /*****************************************************************************
1117 *****************************************************************************/
1118 static void DvdReadHandleDSI( demux_t *p_demux, uint8_t *p_data )
1120 demux_sys_t *p_sys = p_demux->p_sys;
1122 navRead_DSI( &p_sys->dsi_pack, &p_data[DSI_START_BYTE] );
1125 * Determine where we go next. These values are the ones we mostly
1128 p_sys->i_cur_block = p_sys->dsi_pack.dsi_gi.nv_pck_lbn;
1129 p_sys->i_pack_len = p_sys->dsi_pack.dsi_gi.vobu_ea;
1132 * Store the timecodes so we can get the current time
1134 p_sys->i_title_cur_time = (mtime_t) p_sys->dsi_pack.dsi_gi.nv_pck_scr / 90 * 1000;
1135 p_sys->i_cell_cur_time = (mtime_t) dvdtime_to_time( &p_sys->dsi_pack.dsi_gi.c_eltm, 0 );
1138 * If we're not at the end of this cell, we can determine the next
1139 * VOBU to display using the VOBU_SRI information section of the
1140 * DSI. Using this value correctly follows the current angle,
1141 * avoiding the doubled scenes in The Matrix, and makes our life
1145 p_sys->i_next_vobu = p_sys->i_cur_block +
1146 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1148 if( p_sys->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL
1149 && p_sys->i_angle > 1 )
1151 switch( ( p_sys->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1154 /* Interleaved unit with no angle */
1155 if( p_sys->dsi_pack.sml_pbi.ilvu_sa != 0 )
1157 p_sys->i_next_vobu = p_sys->i_cur_block +
1158 p_sys->dsi_pack.sml_pbi.ilvu_sa;
1159 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1163 p_sys->i_next_vobu = p_sys->i_cur_block +
1164 p_sys->dsi_pack.dsi_gi.vobu_ea + 1;
1168 /* vobu is end of ilvu */
1169 if( p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address )
1171 p_sys->i_next_vobu = p_sys->i_cur_block +
1172 p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address;
1173 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1178 /* vobu is beginning of ilvu */
1182 /* entering interleaved section */
1184 /* non interleaved cells in interleaved section */
1186 p_sys->i_next_vobu = p_sys->i_cur_block +
1187 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1191 else if( p_sys->dsi_pack.vobu_sri.next_vobu == SRI_END_OF_CELL )
1193 p_sys->i_cur_cell = p_sys->i_next_cell;
1196 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells ) return;
1198 DvdReadFindCell( p_demux );
1200 p_sys->i_next_vobu =
1201 p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1203 p_sys->i_cell_duration = (mtime_t)dvdtime_to_time( &p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].playback_time, 0 );
1208 msg_Dbg( p_demux, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d c_time %lld",
1209 p_sys->dsi_pack.dsi_gi.nv_pck_scr,
1210 p_sys->dsi_pack.dsi_gi.nv_pck_lbn,
1211 p_sys->dsi_pack.dsi_gi.vobu_ea,
1212 p_sys->dsi_pack.dsi_gi.vobu_vob_idn,
1213 p_sys->dsi_pack.dsi_gi.vobu_c_idn,
1214 dvdtime_to_time( &p_sys->dsi_pack.dsi_gi.c_eltm, 0 ) );
1216 msg_Dbg( p_demux, "cell duration: %lld",
1217 (mtime_t)dvdtime_to_time( &p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].playback_time, 0 ) );
1219 msg_Dbg( p_demux, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1220 p_sys->dsi_pack.sml_pbi.category,
1221 p_sys->dsi_pack.sml_pbi.ilvu_ea,
1222 p_sys->dsi_pack.sml_pbi.ilvu_sa,
1223 p_sys->dsi_pack.sml_pbi.size );
1225 msg_Dbg( p_demux, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1226 p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1227 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle - 1 ].address,
1228 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle ].address);
1232 /*****************************************************************************
1234 *****************************************************************************/
1235 static void DvdReadFindCell( demux_t *p_demux )
1237 demux_sys_t *p_sys = p_demux->p_sys;
1243 #define cell p_sys->p_cur_pgc->cell_playback
1245 if( cell[p_sys->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1247 p_sys->i_cur_cell += p_sys->i_angle - 1;
1249 while( cell[p_sys->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1253 p_sys->i_next_cell = p_sys->i_cur_cell + i + 1;
1257 p_sys->i_next_cell = p_sys->i_cur_cell + 1;
1262 if( p_sys->i_chapter + 1 >= p_sys->i_chapters ) return;
1264 pgc_id = p_sys->p_vts_file->vts_ptt_srpt->title[
1265 p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgcn;
1266 pgn = p_sys->p_vts_file->vts_ptt_srpt->title[
1267 p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgn;
1268 p_pgc = p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
1270 if( p_sys->i_cur_cell >= p_pgc->program_map[pgn - 1] - 1 )
1274 if( p_sys->i_chapter < p_sys->i_chapters &&
1275 p_demux->info.i_seekpoint != p_sys->i_chapter )
1277 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1278 p_demux->info.i_seekpoint = p_sys->i_chapter;
1283 /*****************************************************************************
1284 * DemuxTitles: get the titles/chapters structure
1285 *****************************************************************************/
1286 static void DemuxTitles( demux_t *p_demux, int *pi_angle )
1288 demux_sys_t *p_sys = p_demux->p_sys;
1294 /* Find out number of titles/chapters */
1295 #define tt_srpt p_sys->p_vmg_file->tt_srpt
1297 i_titles = tt_srpt->nr_of_srpts;
1298 msg_Dbg( p_demux, "number of titles: %d", i_titles );
1300 for( i = 0; i < i_titles; i++ )
1302 int32_t i_chapters = 0;
1305 i_chapters = tt_srpt->title[i].nr_of_ptts;
1306 msg_Dbg( p_demux, "title %d has %d chapters", i, i_chapters );
1308 t = vlc_input_title_New();
1310 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
1312 s = vlc_seekpoint_New();
1313 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1316 TAB_APPEND( p_sys->i_titles, p_sys->titles, t );