1 /*****************************************************************************
2 * dvdread.c : DvdRead input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
7 * Authors: St�hane 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 "Default DVD angle." )
61 #define CACHING_TEXT N_("Caching value in ms")
62 #define CACHING_LONGTEXT N_( \
63 "Caching value for DVDs. " \
64 "This value should be set in milliseconds." )
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;
139 /* Current title start/end blocks */
140 int i_title_start_block;
141 int i_title_end_block;
144 mtime_t i_title_cur_time;
146 int i_title_start_cell;
147 int i_title_end_cell;
150 mtime_t i_cell_cur_time;
151 mtime_t i_cell_duration;
154 ps_track_t tk[PS_TK_COUNT];
157 input_title_t **titles;
166 static int Control ( demux_t *, int, va_list );
167 static int Demux ( demux_t * );
168 static int DemuxBlock( demux_t *, uint8_t *, int );
170 static void DemuxTitles( demux_t *, int * );
171 static void ESNew( demux_t *, int, int );
173 static int DvdReadSetArea ( demux_t *, int, int, int );
174 static void DvdReadSeek ( demux_t *, int );
175 static void DvdReadHandleDSI( demux_t *, uint8_t * );
176 static void DvdReadFindCell ( demux_t * );
178 /*****************************************************************************
180 *****************************************************************************/
181 static int Open( vlc_object_t *p_this )
183 demux_t *p_demux = (demux_t*)p_this;
186 char *psz_dvdcss_env;
187 dvd_reader_t *p_dvdread;
188 ifo_handle_t *p_vmg_file;
191 if( !p_demux->psz_path || !*p_demux->psz_path )
193 /* Only when selected */
194 if( !p_this->b_force ) return VLC_EGENERIC;
196 psz_name = var_CreateGetString( p_this, "dvd" );
199 psz_name = strdup("");
203 psz_name = strdup( p_demux->psz_path );
206 if( psz_name[0] && psz_name[1] == ':' &&
207 psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
210 /* Override environment variable DVDCSS_METHOD with config option
211 * (FIXME: this creates a small memory leak) */
212 psz_dvdcss_env = config_GetPsz( p_demux, "dvdread-css-method" );
213 if( psz_dvdcss_env && *psz_dvdcss_env )
217 psz_env = malloc( strlen("DVDCSS_METHOD=") +
218 strlen( psz_dvdcss_env ) + 1 );
221 free( psz_dvdcss_env );
225 sprintf( psz_env, "%s%s", "DVDCSS_METHOD=", psz_dvdcss_env );
229 if( psz_dvdcss_env ) free( psz_dvdcss_env );
232 if( !(p_dvdread = DVDOpen( psz_name )) )
234 msg_Err( p_demux, "DVDRead cannot open source: %s", psz_name );
240 /* Ifo allocation & initialisation */
241 if( !( p_vmg_file = ifoOpen( p_dvdread, 0 ) ) )
243 msg_Warn( p_demux, "cannot open VMG info" );
246 msg_Dbg( p_demux, "VMG opened" );
248 /* Fill p_demux field */
249 STANDARD_DEMUX_INIT; p_sys = p_demux->p_sys;
251 ps_track_init( p_sys->tk );
252 p_sys->i_aspect = -1;
253 p_sys->i_title_cur_time = (mtime_t) 0;
254 p_sys->i_cell_cur_time = (mtime_t) 0;
255 p_sys->i_cell_duration = (mtime_t) 0;
257 p_sys->p_dvdread = p_dvdread;
258 p_sys->p_vmg_file = p_vmg_file;
259 p_sys->p_title = NULL;
260 p_sys->p_vts_file = NULL;
262 p_sys->i_title = p_sys->i_chapter = -1;
263 p_sys->i_mux_rate = 0;
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_demux->info.i_title >= 0 && p_demux->info.i_title < p_sys->i_titles )
389 *pi64 = (int64_t) dvdtime_to_time( &p_sys->p_cur_pgc->playback_time, 0 ) /
390 p_sys->i_title_blocks * p_sys->i_title_offset;
396 case DEMUX_GET_LENGTH:
397 pi64 = (int64_t*)va_arg( args, int64_t * );
398 if( p_demux->info.i_title >= 0 && p_demux->info.i_title < p_sys->i_titles )
400 *pi64 = (int64_t)dvdtime_to_time( &p_sys->p_cur_pgc->playback_time, 0 );
406 /* Special for access_demux */
407 case DEMUX_CAN_PAUSE:
408 case DEMUX_CAN_CONTROL_PACE:
410 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
414 case DEMUX_SET_PAUSE_STATE:
417 case DEMUX_GET_TITLE_INFO:
418 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
419 pi_int = (int*)va_arg( args, int* );
420 *((int*)va_arg( args, int* )) = 1; /* Title offset */
421 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
423 /* Duplicate title infos */
424 *pi_int = p_sys->i_titles;
425 *ppp_title = malloc( sizeof(input_title_t **) * p_sys->i_titles );
426 for( i = 0; i < p_sys->i_titles; i++ )
428 (*ppp_title)[i] = vlc_input_title_Duplicate(p_sys->titles[i]);
432 case DEMUX_SET_TITLE:
433 i = (int)va_arg( args, int );
434 if( DvdReadSetArea( p_demux, i, 0, -1 ) != VLC_SUCCESS )
436 msg_Warn( p_demux, "cannot set title/chapter" );
439 p_demux->info.i_update |=
440 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
441 p_demux->info.i_title = i;
442 p_demux->info.i_seekpoint = 0;
445 case DEMUX_SET_SEEKPOINT:
446 i = (int)va_arg( args, int );
447 if( DvdReadSetArea( p_demux, -1, i, -1 ) != VLC_SUCCESS )
449 msg_Warn( p_demux, "cannot set title/chapter" );
452 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
453 p_demux->info.i_seekpoint = i;
456 case DEMUX_GET_PTS_DELAY:
457 pi64 = (int64_t*)va_arg( args, int64_t * );
458 *pi64 = (int64_t)var_GetInteger( p_demux, "dvdread-caching" )*1000;
461 /* TODO implement others */
467 /*****************************************************************************
469 *****************************************************************************/
470 static int Demux( demux_t *p_demux )
472 demux_sys_t *p_sys = p_demux->p_sys;
474 uint8_t p_buffer[DVD_VIDEO_LB_LEN * DVD_BLOCK_READ_ONCE];
475 int i_blocks_once, i_read;
479 * Playback by cell in this pgc, starting at the cell for our chapter.
483 * Check end of pack, and select the following one
485 if( !p_sys->i_pack_len )
487 /* Read NAV packet */
488 if( DVDReadBlocks( p_sys->p_title, p_sys->i_next_vobu,
491 msg_Err( p_demux, "read failed for block %d", p_sys->i_next_vobu );
495 /* Basic check to be sure we don't have a empty title
496 * go to next title if so */
497 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
499 /* Parse the contained dsi packet */
500 DvdReadHandleDSI( p_demux, p_buffer );
503 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells )
505 if( p_sys->i_title + 1 >= p_sys->i_titles )
510 DvdReadSetArea( p_demux, p_sys->i_title + 1, 0, -1 );
513 if( p_sys->i_pack_len >= 1024 )
515 msg_Err( p_demux, "i_pack_len >= 1024 (%i). "
516 "This shouldn't happen!", p_sys->i_pack_len );
520 /* FIXME: Ugly kludge: we send the pack block to the input for it
521 * sometimes has a zero scr and restart the sync */
522 p_sys->i_cur_block++;
523 p_sys->i_title_offset++;
525 DemuxBlock( p_demux, p_buffer, DVD_VIDEO_LB_LEN );
528 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells )
530 if( p_sys->i_title + 1 >= p_sys->i_titles )
535 DvdReadSetArea( p_demux, p_sys->i_title + 1, 0, -1 );
541 i_blocks_once = __MIN( p_sys->i_pack_len, DVD_BLOCK_READ_ONCE );
542 p_sys->i_pack_len -= i_blocks_once;
545 i_read = DVDReadBlocks( p_sys->p_title, p_sys->i_cur_block,
546 i_blocks_once, p_buffer );
547 if( i_read != i_blocks_once )
549 msg_Err( p_demux, "read failed for %d/%d blocks at 0x%02x",
550 i_read, i_blocks_once, p_sys->i_cur_block );
554 p_sys->i_cur_block += i_read;
555 p_sys->i_title_offset += i_read;
558 msg_Dbg( p_demux, "i_blocks: %d len: %d current: 0x%02x",
559 i_read, p_sys->i_pack_len, p_sys->i_cur_block );
562 for( i = 0; i < i_read; i++ )
564 DemuxBlock( p_demux, p_buffer + i * DVD_VIDEO_LB_LEN,
573 /*****************************************************************************
574 * DemuxBlock: demux a given block
575 *****************************************************************************/
576 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
578 demux_sys_t *p_sys = p_demux->p_sys;
581 while( p < &pkt[i_pkt] )
583 int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
591 p_pkt = block_New( p_demux, i_size );
592 memcpy( p_pkt->p_buffer, p, i_size);
594 /* Parse it and send it */
595 switch( 0x100 | p[3] )
604 msg_Warn( p_demux, "received a PSM packet" );
606 else if( p[3] == 0xbb )
608 msg_Warn( p_demux, "received a SYSTEM packet" );
611 block_Release( p_pkt );
618 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
620 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
621 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
623 block_Release( p_pkt );
628 int i_id = ps_pkt_id( p_pkt );
631 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
635 ESNew( p_demux, i_id, 0 );
637 if( tk->b_seen && tk->es &&
638 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
640 es_out_Send( p_demux->out, tk->es, p_pkt );
644 block_Release( p_pkt );
649 block_Release( p_pkt );
661 /*****************************************************************************
662 * ESNew: register a new elementary stream
663 *****************************************************************************/
664 static void ESNew( demux_t *p_demux, int i_id, int i_lang )
666 demux_sys_t *p_sys = p_demux->p_sys;
667 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
668 char psz_language[3];
670 if( tk->b_seen ) return;
672 if( ps_track_fill( tk, 0, i_id ) )
674 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
678 psz_language[0] = psz_language[1] = psz_language[2] = 0;
679 if( i_lang && i_lang != 0xffff )
681 psz_language[0] = (i_lang >> 8)&0xff;
682 psz_language[1] = (i_lang )&0xff;
686 if( tk->fmt.i_cat == VIDEO_ES )
688 if( p_sys->i_aspect >= 0 )
690 tk->fmt.video.i_aspect = p_sys->i_aspect;
693 else if( tk->fmt.i_cat == AUDIO_ES )
696 /* find the audio number PLEASE find another way */
697 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
701 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
705 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
709 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
714 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
716 else if( tk->fmt.i_cat == SPU_ES )
719 tk->fmt.subs.spu.palette[0] = 0xBeef;
720 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
721 16 * sizeof( uint32_t ) );
723 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
726 tk->es = es_out_Add( p_demux->out, &tk->fmt );
727 tk->b_seen = VLC_TRUE;
730 /*****************************************************************************
731 * DvdReadSetArea: initialize input data for title x, chapter y.
732 * It should be called for each user navigation request.
733 *****************************************************************************
734 * Take care that i_title and i_chapter start from 0.
735 *****************************************************************************/
736 static int DvdReadSetArea( demux_t *p_demux, int i_title, int i_chapter,
739 demux_sys_t *p_sys = p_demux->p_sys;
740 int pgc_id = 0, pgn = 0;
743 #define p_pgc p_sys->p_cur_pgc
744 #define p_vmg p_sys->p_vmg_file
745 #define p_vts p_sys->p_vts_file
747 if( i_title >= 0 && i_title < p_sys->i_titles &&
748 i_title != p_sys->i_title )
750 int i_start_cell, i_end_cell;
752 if( p_sys->p_title != NULL ) DVDCloseFile( p_sys->p_title );
753 if( p_vts != NULL ) ifoClose( p_vts );
754 p_sys->i_title = i_title;
757 * We have to load all title information
759 msg_Dbg( p_demux, "open VTS %d, for title %d",
760 p_vmg->tt_srpt->title[i_title].title_set_nr, i_title + 1 );
763 if( !( p_vts = ifoOpen( p_sys->p_dvdread,
764 p_vmg->tt_srpt->title[i_title].title_set_nr ) ) )
766 msg_Err( p_demux, "fatal error in vts ifo" );
770 /* Title position inside the selected vts */
771 p_sys->i_ttn = p_vmg->tt_srpt->title[i_title].vts_ttn;
773 /* Find title start/end */
774 pgc_id = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgcn;
775 pgn = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgn;
776 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
778 p_sys->i_title_start_cell =
779 i_start_cell = p_pgc->program_map[pgn - 1] - 1;
780 p_sys->i_title_start_block =
781 p_pgc->cell_playback[i_start_cell].first_sector;
783 p_sys->i_title_end_cell =
784 i_end_cell = p_pgc->nr_of_cells - 1;
785 p_sys->i_title_end_block =
786 p_pgc->cell_playback[i_end_cell].last_sector;
788 p_sys->i_title_offset = 0;
790 p_sys->i_title_blocks = 0;
791 for( i = i_start_cell; i <= i_end_cell; i++ )
793 p_sys->i_title_blocks += p_pgc->cell_playback[i].last_sector -
794 p_pgc->cell_playback[i].first_sector + 1;
797 msg_Dbg( p_demux, "title %d vts_title %d pgc %d pgn %d "
798 "start %d end %d blocks: %d",
799 i_title + 1, p_sys->i_ttn, pgc_id, pgn,
800 p_sys->i_title_start_block, p_sys->i_title_end_block,
801 p_sys->i_title_blocks );
804 * Set properties for current chapter
806 p_sys->i_chapter = 0;
808 p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].nr_of_ptts;
810 pgc_id = p_vts->vts_ptt_srpt->title[
811 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgcn;
812 pgn = p_vts->vts_ptt_srpt->title[
813 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgn;
815 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
816 p_sys->i_pack_len = 0;
818 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
819 DvdReadFindCell( p_demux );
821 p_sys->i_next_vobu = p_sys->i_cur_block =
822 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
827 p_sys->i_angles = p_vmg->tt_srpt->title[i_title].nr_of_angles;
828 if( p_sys->i_angle > p_sys->i_angles ) p_sys->i_angle = 1;
831 * We've got enough info, time to open the title set data.
833 if( !( p_sys->p_title = DVDOpenFile( p_sys->p_dvdread,
834 p_vmg->tt_srpt->title[i_title].title_set_nr,
835 DVD_READ_TITLE_VOBS ) ) )
837 msg_Err( p_demux, "cannot open title (VTS_%02d_1.VOB)",
838 p_vmg->tt_srpt->title[i_title].title_set_nr );
842 //IfoPrintTitle( p_demux );
845 * Destroy obsolete ES by reinitializing program 0
846 * and find all ES in title with ifo data
848 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
850 for( i = 0; i < PS_TK_COUNT; i++ )
852 ps_track_t *tk = &p_sys->tk[i];
855 es_format_Clean( &tk->fmt );
856 if( tk->es ) es_out_Del( p_demux->out, tk->es );
858 tk->b_seen = VLC_FALSE;
861 if( p_demux->info.i_title != i_title )
863 p_demux->info.i_update |=
864 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
865 p_demux->info.i_title = i_title;
866 p_demux->info.i_seekpoint = 0;
869 /* TODO: re-add angles */
872 ESNew( p_demux, 0xe0, 0 ); /* Video, FIXME ? */
873 p_sys->i_aspect = p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio;
875 #define audio_control \
876 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
878 /* Audio ES, in the order they appear in the .ifo */
879 for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
884 //IfoPrintAudio( p_demux, i );
886 /* Audio channel is active if first byte is 0x80 */
887 if( audio_control & 0x8000 )
889 i_position = ( audio_control & 0x7F00 ) >> 8;
891 msg_Dbg( p_demux, "audio position %d", i_position );
892 switch( p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format )
895 i_id = (0x80 + i_position) | 0xbd00;
898 case 0x03: /* MPEG audio */
899 i_id = 0xc000 + i_position;
901 case 0x04: /* LPCM */
902 i_id = (0xa0 + i_position) | 0xbd00;
905 i_id = (0x88 + i_position) | 0xbd00;
909 msg_Err( p_demux, "unknown audio type %.2x",
910 p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format );
913 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
914 vts_audio_attr[i - 1].lang_code );
919 #define spu_palette \
920 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette
922 memcpy( p_sys->clut, spu_palette, 16 * sizeof( uint32_t ) );
924 #define spu_control \
925 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
928 for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
933 //IfoPrintSpu( p_sys, i );
934 msg_Dbg( p_demux, "spu %d 0x%02x", i, spu_control );
936 if( spu_control & 0x80000000 )
938 /* there are several streams for one spu */
939 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
942 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
944 case 1: /* letterbox */
945 i_position = spu_control & 0xff;
947 case 2: /* pan&scan */
948 i_position = ( spu_control >> 8 ) & 0xff;
950 default: /* widescreen */
951 i_position = ( spu_control >> 16 ) & 0xff;
958 i_position = ( spu_control >> 24 ) & 0x7F;
961 i_id = (0x20 + i_position) | 0xbd00;
963 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
964 vts_subp_attr[i - 1].lang_code );
970 else if( i_title != -1 && i_title != p_sys->i_title )
973 return VLC_EGENERIC; /* Couldn't set title */
980 if( i_chapter >= 0 && i_chapter < p_sys->i_chapters )
982 pgc_id = p_vts->vts_ptt_srpt->title[
983 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
984 pgn = p_vts->vts_ptt_srpt->title[
985 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
987 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
989 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
990 p_sys->i_chapter = i_chapter;
991 DvdReadFindCell( p_demux );
993 p_sys->i_title_offset = 0;
994 for( i = p_sys->i_title_start_cell; i < p_sys->i_cur_cell; i++ )
996 p_sys->i_title_offset += p_pgc->cell_playback[i].last_sector -
997 p_pgc->cell_playback[i].first_sector + 1;
1000 p_sys->i_pack_len = 0;
1001 p_sys->i_next_vobu = p_sys->i_cur_block =
1002 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1004 if( p_demux->info.i_seekpoint != i_chapter )
1006 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1007 p_demux->info.i_seekpoint = i_chapter;
1010 else if( i_chapter != -1 )
1013 return VLC_EGENERIC; /* Couldn't set chapter */
1023 /*****************************************************************************
1024 * DvdReadSeek : Goes to a given position on the stream.
1025 *****************************************************************************
1026 * This one is used by the input and translate chronological position from
1027 * input to logical position on the device.
1028 *****************************************************************************/
1029 static void DvdReadSeek( demux_t *p_demux, int i_block_offset )
1031 demux_sys_t *p_sys = p_demux->p_sys;
1038 #define p_pgc p_sys->p_cur_pgc
1039 #define p_vts p_sys->p_vts_file
1042 i_block = i_block_offset;
1043 for( i_cell = p_sys->i_title_start_cell;
1044 i_cell <= p_sys->i_title_end_cell; i_cell++ )
1046 if( i_block < (int)p_pgc->cell_playback[i_cell].last_sector -
1047 (int)p_pgc->cell_playback[i_cell].first_sector + 1 ) break;
1049 i_block -= (p_pgc->cell_playback[i_cell].last_sector -
1050 p_pgc->cell_playback[i_cell].first_sector + 1);
1052 if( i_cell > p_sys->i_title_end_cell )
1054 msg_Err( p_demux, "couldn't find cell for block %i", i_block_offset );
1057 i_block += p_pgc->cell_playback[i_cell].first_sector;
1058 p_sys->i_title_offset = i_block_offset;
1061 for( i_chapter = 0; i_chapter < p_sys->i_chapters; i_chapter++ )
1063 int pgc_id, pgn, i_tmp;
1065 pgc_id = p_vts->vts_ptt_srpt->title[
1066 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
1067 pgn = p_vts->vts_ptt_srpt->title[
1068 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
1070 i_tmp = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc->program_map[pgn-1];
1072 if( i_tmp > i_cell ) break;
1075 if( i_chapter < p_sys->i_chapters &&
1076 p_demux->info.i_seekpoint != i_chapter )
1078 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1079 p_demux->info.i_seekpoint = i_chapter;
1083 while( (int)p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu] <= i_block )
1089 while( p_vts->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1090 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1096 msg_Dbg( p_demux, "cell %d i_sub_cell %d chapter %d vobu %d "
1097 "cell_sector %d vobu_sector %d sub_cell_sector %d",
1098 i_cell, i_sub_cell, i_chapter, i_vobu,
1099 p_sys->p_cur_pgc->cell_playback[i_cell].first_sector,
1100 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu],
1101 p_vts->vts_c_adt->cell_adr_table[i_sub_cell - 1].start_sector);
1104 p_sys->i_cur_block = i_block;
1105 p_sys->i_next_vobu = p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu];
1106 p_sys->i_pack_len = p_sys->i_next_vobu - i_block;
1107 p_sys->i_cur_cell = i_cell;
1108 p_sys->i_chapter = i_chapter;
1109 DvdReadFindCell( p_demux );
1117 /*****************************************************************************
1119 *****************************************************************************/
1120 static void DvdReadHandleDSI( demux_t *p_demux, uint8_t *p_data )
1122 demux_sys_t *p_sys = p_demux->p_sys;
1124 navRead_DSI( &p_sys->dsi_pack, &p_data[DSI_START_BYTE] );
1127 * Determine where we go next. These values are the ones we mostly
1130 p_sys->i_cur_block = p_sys->dsi_pack.dsi_gi.nv_pck_lbn;
1131 p_sys->i_pack_len = p_sys->dsi_pack.dsi_gi.vobu_ea;
1134 * Store the timecodes so we can get the current time
1136 p_sys->i_title_cur_time = (mtime_t) (p_sys->dsi_pack.dsi_gi.nv_pck_scr / 90 * 1000);
1137 p_sys->i_cell_cur_time = (mtime_t) dvdtime_to_time( &p_sys->dsi_pack.dsi_gi.c_eltm, 0 );
1140 * If we're not at the end of this cell, we can determine the next
1141 * VOBU to display using the VOBU_SRI information section of the
1142 * DSI. Using this value correctly follows the current angle,
1143 * avoiding the doubled scenes in The Matrix, and makes our life
1147 p_sys->i_next_vobu = p_sys->i_cur_block +
1148 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1150 if( p_sys->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL
1151 && p_sys->i_angle > 1 )
1153 switch( ( p_sys->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1156 /* Interleaved unit with no angle */
1157 if( p_sys->dsi_pack.sml_pbi.ilvu_sa != 0 )
1159 p_sys->i_next_vobu = p_sys->i_cur_block +
1160 p_sys->dsi_pack.sml_pbi.ilvu_sa;
1161 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1165 p_sys->i_next_vobu = p_sys->i_cur_block +
1166 p_sys->dsi_pack.dsi_gi.vobu_ea + 1;
1170 /* vobu is end of ilvu */
1171 if( p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address )
1173 p_sys->i_next_vobu = p_sys->i_cur_block +
1174 p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address;
1175 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1180 /* vobu is beginning of ilvu */
1184 /* entering interleaved section */
1186 /* non interleaved cells in interleaved section */
1188 p_sys->i_next_vobu = p_sys->i_cur_block +
1189 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1193 else if( p_sys->dsi_pack.vobu_sri.next_vobu == SRI_END_OF_CELL )
1195 p_sys->i_cur_cell = p_sys->i_next_cell;
1198 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells ) return;
1200 DvdReadFindCell( p_demux );
1202 p_sys->i_next_vobu =
1203 p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1205 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 );
1210 msg_Dbg( p_demux, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d c_time %lld",
1211 p_sys->dsi_pack.dsi_gi.nv_pck_scr,
1212 p_sys->dsi_pack.dsi_gi.nv_pck_lbn,
1213 p_sys->dsi_pack.dsi_gi.vobu_ea,
1214 p_sys->dsi_pack.dsi_gi.vobu_vob_idn,
1215 p_sys->dsi_pack.dsi_gi.vobu_c_idn,
1216 dvdtime_to_time( &p_sys->dsi_pack.dsi_gi.c_eltm, 0 ) );
1218 msg_Dbg( p_demux, "cell duration: %lld",
1219 (mtime_t)dvdtime_to_time( &p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].playback_time, 0 ) );
1221 msg_Dbg( p_demux, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1222 p_sys->dsi_pack.sml_pbi.category,
1223 p_sys->dsi_pack.sml_pbi.ilvu_ea,
1224 p_sys->dsi_pack.sml_pbi.ilvu_sa,
1225 p_sys->dsi_pack.sml_pbi.size );
1227 msg_Dbg( p_demux, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1228 p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1229 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle - 1 ].address,
1230 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle ].address);
1234 /*****************************************************************************
1236 *****************************************************************************/
1237 static void DvdReadFindCell( demux_t *p_demux )
1239 demux_sys_t *p_sys = p_demux->p_sys;
1245 #define cell p_sys->p_cur_pgc->cell_playback
1247 if( cell[p_sys->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1249 p_sys->i_cur_cell += p_sys->i_angle - 1;
1251 while( cell[p_sys->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1255 p_sys->i_next_cell = p_sys->i_cur_cell + i + 1;
1259 p_sys->i_next_cell = p_sys->i_cur_cell + 1;
1264 if( p_sys->i_chapter + 1 >= p_sys->i_chapters ) return;
1266 pgc_id = p_sys->p_vts_file->vts_ptt_srpt->title[
1267 p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgcn;
1268 pgn = p_sys->p_vts_file->vts_ptt_srpt->title[
1269 p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgn;
1270 p_pgc = p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
1272 if( p_sys->i_cur_cell >= p_pgc->program_map[pgn - 1] - 1 )
1276 if( p_sys->i_chapter < p_sys->i_chapters &&
1277 p_demux->info.i_seekpoint != p_sys->i_chapter )
1279 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1280 p_demux->info.i_seekpoint = p_sys->i_chapter;
1285 /*****************************************************************************
1286 * DemuxTitles: get the titles/chapters structure
1287 *****************************************************************************/
1288 static void DemuxTitles( demux_t *p_demux, int *pi_angle )
1290 demux_sys_t *p_sys = p_demux->p_sys;
1296 /* Find out number of titles/chapters */
1297 #define tt_srpt p_sys->p_vmg_file->tt_srpt
1299 i_titles = tt_srpt->nr_of_srpts;
1300 msg_Dbg( p_demux, "number of titles: %d", i_titles );
1302 for( i = 0; i < i_titles; i++ )
1304 int32_t i_chapters = 0;
1307 i_chapters = tt_srpt->title[i].nr_of_ptts;
1308 msg_Dbg( p_demux, "title %d has %d chapters", i, i_chapters );
1310 t = vlc_input_title_New();
1312 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
1314 s = vlc_seekpoint_New();
1315 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1318 TAB_APPEND( p_sys->i_titles, p_sys->titles, t );