1 /*****************************************************************************
2 * dvdread.c : DvdRead input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2006 VLC authors and 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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * NOTA BENE: this module requires the linking against a library which is
27 * known to require licensing under the GNU General Public License version 2
28 * (or later). Therefore, the result of compiling this module will normally
29 * be subject to the terms of that later license.
30 *****************************************************************************/
33 /*****************************************************************************
35 *****************************************************************************/
41 #include <vlc_common.h>
42 #include <vlc_plugin.h>
43 #include <vlc_input.h>
44 #include <vlc_access.h>
45 #include <vlc_charset.h>
46 #include <vlc_interface.h>
47 #include <vlc_dialog.h>
49 #include <vlc_iso_lang.h>
51 #include "../demux/ps.h"
53 #include <sys/types.h>
56 #include <dvdread/dvd_reader.h>
57 #include <dvdread/ifo_types.h>
58 #include <dvdread/ifo_read.h>
59 #include <dvdread/nav_read.h>
60 #include <dvdread/nav_print.h>
64 /*****************************************************************************
66 *****************************************************************************/
67 #define ANGLE_TEXT N_("DVD angle")
68 #define ANGLE_LONGTEXT N_( \
69 "Default DVD angle." )
71 static int Open ( vlc_object_t * );
72 static void Close( vlc_object_t * );
75 set_shortname( N_("DVD without menus") )
76 set_description( N_("DVDRead Input (no menu support)") )
77 set_category( CAT_INPUT )
78 set_subcategory( SUBCAT_INPUT_ACCESS )
79 add_integer( "dvdread-angle", 1, ANGLE_TEXT,
80 ANGLE_LONGTEXT, false )
81 add_obsolete_string( "dvdread-css-method" ) /* obsolete since 1.1.0 */
82 set_capability( "access_demux", 0 )
83 add_shortcut( "dvd", "dvdread", "dvdsimple" )
84 set_callbacks( Open, Close )
87 /* how many blocks DVDRead will read in each loop */
88 #define DVD_BLOCK_READ_ONCE 4
90 /*****************************************************************************
92 *****************************************************************************/
97 dvd_reader_t *p_dvdread;
100 ifo_handle_t *p_vmg_file;
101 ifo_handle_t *p_vts_file;
104 int i_chapter, i_chapters;
105 int i_angle, i_angles;
107 tt_srpt_t *p_tt_srpt;
118 /* Current title start/end blocks */
119 int i_title_start_block;
120 int i_title_end_block;
123 mtime_t i_title_cur_time;
125 int i_title_start_cell;
126 int i_title_end_cell;
129 mtime_t i_cell_cur_time;
130 mtime_t i_cell_duration;
133 ps_track_t tk[PS_TK_COUNT];
136 input_title_t **titles;
146 static int Control ( demux_t *, int, va_list );
147 static int Demux ( demux_t * );
148 static int DemuxBlock( demux_t *, const uint8_t *, int );
150 static void DemuxTitles( demux_t *, int * );
151 static void ESNew( demux_t *, int, int );
153 static int DvdReadSetArea ( demux_t *, int, int, int );
154 static void DvdReadSeek ( demux_t *, int );
155 static void DvdReadHandleDSI( demux_t *, uint8_t * );
156 static void DvdReadFindCell ( demux_t * );
158 /*****************************************************************************
160 *****************************************************************************/
161 static int Open( vlc_object_t *p_this )
163 demux_t *p_demux = (demux_t*)p_this;
166 ifo_handle_t *p_vmg_file;
168 if( !p_demux->psz_file || !*p_demux->psz_file )
170 /* Only when selected */
171 if( !p_demux->psz_access || !*p_demux->psz_access )
174 psz_file = var_InheritString( p_this, "dvd" );
177 psz_file = strdup( p_demux->psz_file );
179 #if defined( _WIN32 ) || defined( __OS2__ )
180 if( psz_file != NULL )
182 size_t flen = strlen( psz_file );
183 if( flen > 0 && psz_file[flen - 1] == '\\' )
184 psz_file[flen - 1] = '\0';
187 psz_file = strdup("");
189 if( unlikely(psz_file == NULL) )
193 const char *psz_path = ToLocale( psz_file );
194 dvd_reader_t *p_dvdread = DVDOpen( psz_path );
196 LocaleFree( psz_path );
197 if( p_dvdread == NULL )
199 msg_Err( p_demux, "DVDRead cannot open source: %s", psz_file );
200 dialog_Fatal( p_demux, _("Playback failure"),
201 _("DVDRead could not open the disc \"%s\"."), psz_file );
207 /* Ifo allocation & initialisation */
208 if( !( p_vmg_file = ifoOpen( p_dvdread, 0 ) ) )
210 msg_Warn( p_demux, "cannot open VMG info" );
213 msg_Dbg( p_demux, "VMG opened" );
215 /* Fill p_demux field */
216 DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
218 ps_track_init( p_sys->tk );
219 p_sys->i_sar_num = 0;
220 p_sys->i_sar_den = 0;
221 p_sys->i_title_cur_time = (mtime_t) 0;
222 p_sys->i_cell_cur_time = (mtime_t) 0;
223 p_sys->i_cell_duration = (mtime_t) 0;
225 p_sys->p_dvdread = p_dvdread;
226 p_sys->p_vmg_file = p_vmg_file;
227 p_sys->p_title = NULL;
228 p_sys->p_vts_file = NULL;
230 p_sys->i_title = p_sys->i_chapter = -1;
231 p_sys->i_mux_rate = 0;
233 p_sys->i_angle = var_CreateGetInteger( p_demux, "dvdread-angle" );
234 if( p_sys->i_angle <= 0 ) p_sys->i_angle = 1;
236 DemuxTitles( p_demux, &p_sys->i_angle );
237 if( DvdReadSetArea( p_demux, 0, 0, p_sys->i_angle ) != VLC_SUCCESS )
239 msg_Err( p_demux, "DvdReadSetArea(0,0,%i) failed (can't decrypt DVD?)",
248 /*****************************************************************************
250 *****************************************************************************/
251 static void Close( vlc_object_t *p_this )
253 demux_t *p_demux = (demux_t*)p_this;
254 demux_sys_t *p_sys = p_demux->p_sys;
257 for( i = 0; i < PS_TK_COUNT; i++ )
259 ps_track_t *tk = &p_sys->tk[i];
262 es_format_Clean( &tk->fmt );
263 if( tk->es ) es_out_Del( p_demux->out, tk->es );
267 /* Free the array of titles */
268 for( int i = 0; i < p_sys->i_titles; i++ )
269 vlc_input_title_Delete( p_sys->titles[i] );
270 TAB_CLEAN( p_sys->i_titles, p_sys->titles );
272 /* Close libdvdread */
273 if( p_sys->p_title ) DVDCloseFile( p_sys->p_title );
274 if( p_sys->p_vts_file ) ifoClose( p_sys->p_vts_file );
275 if( p_sys->p_vmg_file ) ifoClose( p_sys->p_vmg_file );
276 DVDClose( p_sys->p_dvdread );
281 static int64_t dvdtime_to_time( dvd_time_t *dtime, uint8_t still_time )
283 /* Macro to convert Binary Coded Decimal to Decimal */
284 #define BCD2D(__x__) (((__x__ & 0xf0) >> 4) * 10 + (__x__ & 0x0f))
287 int64_t i_micro_second = 0;
289 if (still_time == 0 || still_time == 0xFF)
291 i_micro_second += (int64_t)(BCD2D(dtime->hour)) * 60 * 60 * 1000000;
292 i_micro_second += (int64_t)(BCD2D(dtime->minute)) * 60 * 1000000;
293 i_micro_second += (int64_t)(BCD2D(dtime->second)) * 1000000;
295 switch((dtime->frame_u & 0xc0) >> 6)
307 f_ms = BCD2D(dtime->frame_u&0x3f) * 1000.0 / f_fps;
308 i_micro_second += (int64_t)(f_ms * 1000.0);
312 i_micro_second = still_time;
313 i_micro_second = (int64_t)((double)i_micro_second * 1000000.0);
316 return i_micro_second;
319 /*****************************************************************************
321 *****************************************************************************/
322 static int Control( demux_t *p_demux, int i_query, va_list args )
324 demux_sys_t *p_sys = p_demux->p_sys;
328 input_title_t ***ppp_title;
334 case DEMUX_GET_POSITION:
336 pf = (double*) va_arg( args, double* );
338 if( p_sys->i_title_blocks > 0 )
339 *pf = (double)p_sys->i_title_offset / p_sys->i_title_blocks;
345 case DEMUX_SET_POSITION:
347 f = (double)va_arg( args, double );
349 DvdReadSeek( p_demux, f * p_sys->i_title_blocks );
354 pi64 = (int64_t*)va_arg( args, int64_t * );
355 if( p_demux->info.i_title >= 0 && p_demux->info.i_title < p_sys->i_titles )
357 *pi64 = (int64_t) dvdtime_to_time( &p_sys->p_cur_pgc->playback_time, 0 ) /
358 p_sys->i_title_blocks * p_sys->i_title_offset;
364 case DEMUX_GET_LENGTH:
365 pi64 = (int64_t*)va_arg( args, int64_t * );
366 if( p_demux->info.i_title >= 0 && p_demux->info.i_title < p_sys->i_titles )
368 *pi64 = (int64_t)dvdtime_to_time( &p_sys->p_cur_pgc->playback_time, 0 );
374 /* Special for access_demux */
375 case DEMUX_CAN_PAUSE:
377 case DEMUX_CAN_CONTROL_PACE:
379 pb = (bool*)va_arg( args, bool * );
383 case DEMUX_SET_PAUSE_STATE:
386 case DEMUX_GET_TITLE_INFO:
387 ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
388 pi_int = (int*)va_arg( args, int* );
389 *((int*)va_arg( args, int* )) = 1; /* Title offset */
390 *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
392 /* Duplicate title infos */
393 *pi_int = p_sys->i_titles;
394 *ppp_title = malloc( p_sys->i_titles * sizeof(input_title_t *) );
395 for( i = 0; i < p_sys->i_titles; i++ )
397 (*ppp_title)[i] = vlc_input_title_Duplicate(p_sys->titles[i]);
401 case DEMUX_SET_TITLE:
402 i = (int)va_arg( args, int );
403 if( DvdReadSetArea( p_demux, i, 0, -1 ) != VLC_SUCCESS )
405 msg_Warn( p_demux, "cannot set title/chapter" );
408 p_demux->info.i_update |=
409 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
410 p_demux->info.i_title = i;
411 p_demux->info.i_seekpoint = 0;
414 case DEMUX_SET_SEEKPOINT:
415 i = (int)va_arg( args, int );
416 if( DvdReadSetArea( p_demux, -1, i, -1 ) != VLC_SUCCESS )
418 msg_Warn( p_demux, "cannot set title/chapter" );
421 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
422 p_demux->info.i_seekpoint = i;
425 case DEMUX_GET_PTS_DELAY:
426 pi64 = (int64_t*)va_arg( args, int64_t * );
428 INT64_C(1000) * var_InheritInteger( p_demux, "disc-caching" );
431 /* TODO implement others */
437 /*****************************************************************************
439 *****************************************************************************/
440 static int Demux( demux_t *p_demux )
442 demux_sys_t *p_sys = p_demux->p_sys;
444 uint8_t p_buffer[DVD_VIDEO_LB_LEN * DVD_BLOCK_READ_ONCE];
445 int i_blocks_once, i_read;
449 * Playback by cell in this pgc, starting at the cell for our chapter.
453 * Check end of pack, and select the following one
455 if( !p_sys->i_pack_len )
457 /* Read NAV packet */
458 if( DVDReadBlocks( p_sys->p_title, p_sys->i_next_vobu,
461 msg_Err( p_demux, "read failed for block %d", p_sys->i_next_vobu );
462 dialog_Fatal( p_demux, _("Playback failure"),
463 _("DVDRead could not read block %d."),
464 p_sys->i_next_vobu );
468 /* Basic check to be sure we don't have a empty title
469 * go to next title if so */
470 //assert( p_buffer[41] == 0xbf && p_buffer[1027] == 0xbf );
472 /* Parse the contained dsi packet */
473 DvdReadHandleDSI( p_demux, p_buffer );
476 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells )
478 if( p_sys->i_title + 1 >= p_sys->i_titles )
483 DvdReadSetArea( p_demux, p_sys->i_title + 1, 0, -1 );
486 if( p_sys->i_pack_len >= 1024 )
488 msg_Err( p_demux, "i_pack_len >= 1024 (%i). "
489 "This shouldn't happen!", p_sys->i_pack_len );
493 /* FIXME: Ugly kludge: we send the pack block to the input for it
494 * sometimes has a zero scr and restart the sync */
495 p_sys->i_cur_block++;
496 p_sys->i_title_offset++;
498 DemuxBlock( p_demux, p_buffer, DVD_VIDEO_LB_LEN );
501 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells )
503 if( p_sys->i_title + 1 >= p_sys->i_titles )
508 DvdReadSetArea( p_demux, p_sys->i_title + 1, 0, -1 );
514 i_blocks_once = __MIN( p_sys->i_pack_len, DVD_BLOCK_READ_ONCE );
515 p_sys->i_pack_len -= i_blocks_once;
518 i_read = DVDReadBlocks( p_sys->p_title, p_sys->i_cur_block,
519 i_blocks_once, p_buffer );
520 if( i_read != i_blocks_once )
522 msg_Err( p_demux, "read failed for %d/%d blocks at 0x%02x",
523 i_read, i_blocks_once, p_sys->i_cur_block );
524 dialog_Fatal( p_demux, _("Playback failure"),
525 _("DVDRead could not read %d/%d blocks at 0x%02x."),
526 i_read, i_blocks_once, p_sys->i_cur_block );
530 p_sys->i_cur_block += i_read;
531 p_sys->i_title_offset += i_read;
534 msg_Dbg( p_demux, "i_blocks: %d len: %d current: 0x%02x",
535 i_read, p_sys->i_pack_len, p_sys->i_cur_block );
538 for( i = 0; i < i_read; i++ )
540 DemuxBlock( p_demux, p_buffer + i * DVD_VIDEO_LB_LEN,
549 /*****************************************************************************
550 * DemuxBlock: demux a given block
551 *****************************************************************************/
552 static int DemuxBlock( demux_t *p_demux, const uint8_t *p, int len )
554 demux_sys_t *p_sys = p_demux->p_sys;
558 int i_size = ps_pkt_size( p, len );
559 if( i_size <= 0 || i_size > len )
565 block_t *p_pkt = block_Alloc( i_size );
566 memcpy( p_pkt->p_buffer, p, i_size);
568 /* Parse it and send it */
569 switch( 0x100 | p[3] )
578 msg_Warn( p_demux, "received a PSM packet" );
580 else if( p[3] == 0xbb )
582 msg_Warn( p_demux, "received a SYSTEM packet" );
585 block_Release( p_pkt );
592 if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
594 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
595 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
597 block_Release( p_pkt );
602 int i_id = ps_pkt_id( p_pkt );
605 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
609 ESNew( p_demux, i_id, 0 );
611 if( tk->b_seen && tk->es &&
612 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
614 es_out_Send( p_demux->out, tk->es, p_pkt );
618 block_Release( p_pkt );
623 block_Release( p_pkt );
636 /*****************************************************************************
637 * ESNew: register a new elementary stream
638 *****************************************************************************/
639 static void ESNew( demux_t *p_demux, int i_id, int i_lang )
641 demux_sys_t *p_sys = p_demux->p_sys;
642 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
643 char psz_language[3];
645 if( tk->b_seen ) return;
647 if( ps_track_fill( tk, 0, i_id ) )
649 msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
653 psz_language[0] = psz_language[1] = psz_language[2] = 0;
654 if( i_lang && i_lang != 0xffff )
656 psz_language[0] = (i_lang >> 8)&0xff;
657 psz_language[1] = (i_lang )&0xff;
661 if( tk->fmt.i_cat == VIDEO_ES )
663 tk->fmt.video.i_sar_num = p_sys->i_sar_num;
664 tk->fmt.video.i_sar_den = p_sys->i_sar_den;
666 else if( tk->fmt.i_cat == AUDIO_ES )
670 /* find the audio number PLEASE find another way */
671 if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
675 else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
679 else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
683 else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
689 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
691 else if( tk->fmt.i_cat == SPU_ES )
694 tk->fmt.subs.spu.palette[0] = 0xBeef;
695 memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
696 16 * sizeof( uint32_t ) );
698 if( psz_language[0] ) tk->fmt.psz_language = strdup( psz_language );
701 tk->es = es_out_Add( p_demux->out, &tk->fmt );
705 /*****************************************************************************
706 * DvdReadSetArea: initialize input data for title x, chapter y.
707 * It should be called for each user navigation request.
708 *****************************************************************************
709 * Take care that i_title and i_chapter start from 0.
710 *****************************************************************************/
711 static int DvdReadSetArea( demux_t *p_demux, int i_title, int i_chapter,
714 VLC_UNUSED( i_angle );
716 demux_sys_t *p_sys = p_demux->p_sys;
717 int pgc_id = 0, pgn = 0;
720 #define p_pgc p_sys->p_cur_pgc
721 #define p_vmg p_sys->p_vmg_file
722 #define p_vts p_sys->p_vts_file
724 if( i_title >= 0 && i_title < p_sys->i_titles &&
725 i_title != p_sys->i_title )
727 int i_start_cell, i_end_cell;
729 if( p_sys->p_title != NULL ) DVDCloseFile( p_sys->p_title );
730 if( p_vts != NULL ) ifoClose( p_vts );
731 p_sys->i_title = i_title;
734 * We have to load all title information
736 msg_Dbg( p_demux, "open VTS %d, for title %d",
737 p_vmg->tt_srpt->title[i_title].title_set_nr, i_title + 1 );
740 if( !( p_vts = ifoOpen( p_sys->p_dvdread,
741 p_vmg->tt_srpt->title[i_title].title_set_nr ) ) )
743 msg_Err( p_demux, "fatal error in vts ifo" );
747 /* Title position inside the selected vts */
748 p_sys->i_ttn = p_vmg->tt_srpt->title[i_title].vts_ttn;
750 /* Find title start/end */
751 pgc_id = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgcn;
752 pgn = p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].ptt[0].pgn;
753 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
755 if( p_pgc->cell_playback == NULL )
757 msg_Err( p_demux, "Invalid PGC (cell_playback_offset)" );
761 p_sys->i_title_start_cell =
762 i_start_cell = p_pgc->program_map[pgn - 1] - 1;
763 p_sys->i_title_start_block =
764 p_pgc->cell_playback[i_start_cell].first_sector;
766 p_sys->i_title_end_cell =
767 i_end_cell = p_pgc->nr_of_cells - 1;
768 p_sys->i_title_end_block =
769 p_pgc->cell_playback[i_end_cell].last_sector;
771 p_sys->i_title_offset = 0;
773 p_sys->i_title_blocks = 0;
774 for( i = i_start_cell; i <= i_end_cell; i++ )
776 p_sys->i_title_blocks += p_pgc->cell_playback[i].last_sector -
777 p_pgc->cell_playback[i].first_sector + 1;
780 msg_Dbg( p_demux, "title %d vts_title %d pgc %d pgn %d "
781 "start %d end %d blocks: %d",
782 i_title + 1, p_sys->i_ttn, pgc_id, pgn,
783 p_sys->i_title_start_block, p_sys->i_title_end_block,
784 p_sys->i_title_blocks );
787 * Set properties for current chapter
789 p_sys->i_chapter = 0;
791 p_vts->vts_ptt_srpt->title[p_sys->i_ttn - 1].nr_of_ptts;
793 pgc_id = p_vts->vts_ptt_srpt->title[
794 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgcn;
795 pgn = p_vts->vts_ptt_srpt->title[
796 p_sys->i_ttn - 1].ptt[p_sys->i_chapter].pgn;
798 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
799 p_sys->i_pack_len = 0;
801 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
802 DvdReadFindCell( p_demux );
804 p_sys->i_next_vobu = p_sys->i_cur_block =
805 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
810 p_sys->i_angles = p_vmg->tt_srpt->title[i_title].nr_of_angles;
811 if( p_sys->i_angle > p_sys->i_angles ) p_sys->i_angle = 1;
814 * We've got enough info, time to open the title set data.
816 if( !( p_sys->p_title = DVDOpenFile( p_sys->p_dvdread,
817 p_vmg->tt_srpt->title[i_title].title_set_nr,
818 DVD_READ_TITLE_VOBS ) ) )
820 msg_Err( p_demux, "cannot open title (VTS_%02d_1.VOB)",
821 p_vmg->tt_srpt->title[i_title].title_set_nr );
825 //IfoPrintTitle( p_demux );
828 * Destroy obsolete ES by reinitializing program 0
829 * and find all ES in title with ifo data
831 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
833 for( i = 0; i < PS_TK_COUNT; i++ )
835 ps_track_t *tk = &p_sys->tk[i];
838 es_format_Clean( &tk->fmt );
839 if( tk->es ) es_out_Del( p_demux->out, tk->es );
844 if( p_demux->info.i_title != i_title )
846 p_demux->info.i_update |=
847 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
848 p_demux->info.i_title = i_title;
849 p_demux->info.i_seekpoint = 0;
852 /* TODO: re-add angles */
855 ESNew( p_demux, 0xe0, 0 ); /* Video, FIXME ? */
856 const video_attr_t *p_attr = &p_vts->vtsi_mat->vts_video_attr;
857 int i_video_height = p_attr->video_format != 0 ? 576 : 480;
859 switch( p_attr->picture_size )
876 switch( p_attr->display_aspect_ratio )
879 p_sys->i_sar_num = 4 * i_video_height;
880 p_sys->i_sar_den = 3 * i_video_width;
883 p_sys->i_sar_num = 16 * i_video_height;
884 p_sys->i_sar_den = 9 * i_video_width;
887 p_sys->i_sar_num = 0;
888 p_sys->i_sar_den = 0;
892 #define audio_control \
893 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i-1]
895 /* Audio ES, in the order they appear in the .ifo */
896 for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
901 //IfoPrintAudio( p_demux, i );
903 /* Audio channel is active if first byte is 0x80 */
904 if( audio_control & 0x8000 )
906 i_position = ( audio_control & 0x7F00 ) >> 8;
908 msg_Dbg( p_demux, "audio position %d", i_position );
909 switch( p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format )
912 i_id = (0x80 + i_position) | 0xbd00;
915 case 0x03: /* MPEG audio */
916 i_id = 0xc000 + i_position;
918 case 0x04: /* LPCM */
919 i_id = (0xa0 + i_position) | 0xbd00;
922 i_id = (0x88 + i_position) | 0xbd00;
926 msg_Err( p_demux, "unknown audio type %.2x",
927 p_vts->vtsi_mat->vts_audio_attr[i - 1].audio_format );
930 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
931 vts_audio_attr[i - 1].lang_code );
936 #define spu_palette \
937 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette
939 memcpy( p_sys->clut, spu_palette, 16 * sizeof( uint32_t ) );
941 #define spu_control \
942 p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i-1]
945 for( i = 1; i <= p_vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
950 //IfoPrintSpu( p_sys, i );
951 msg_Dbg( p_demux, "spu %d 0x%02x", i, spu_control );
953 if( spu_control & 0x80000000 )
955 /* there are several streams for one spu */
956 if( p_vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
959 switch( p_vts->vtsi_mat->vts_video_attr.permitted_df )
961 case 1: /* letterbox */
962 i_position = spu_control & 0xff;
964 case 2: /* pan&scan */
965 i_position = ( spu_control >> 8 ) & 0xff;
967 default: /* widescreen */
968 i_position = ( spu_control >> 16 ) & 0xff;
975 i_position = ( spu_control >> 24 ) & 0x7F;
978 i_id = (0x20 + i_position) | 0xbd00;
980 ESNew( p_demux, i_id, p_sys->p_vts_file->vtsi_mat->
981 vts_subp_attr[i - 1].lang_code );
987 else if( i_title != -1 && i_title != p_sys->i_title )
990 return VLC_EGENERIC; /* Couldn't set title */
997 if( i_chapter >= 0 && i_chapter < p_sys->i_chapters )
999 pgc_id = p_vts->vts_ptt_srpt->title[
1000 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
1001 pgn = p_vts->vts_ptt_srpt->title[
1002 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
1004 p_pgc = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
1005 if( p_pgc->cell_playback == NULL )
1006 return VLC_EGENERIC; /* Couldn't set chapter */
1008 p_sys->i_cur_cell = p_pgc->program_map[pgn - 1] - 1;
1009 p_sys->i_chapter = i_chapter;
1010 DvdReadFindCell( p_demux );
1012 p_sys->i_title_offset = 0;
1013 for( i = p_sys->i_title_start_cell; i < p_sys->i_cur_cell; i++ )
1015 p_sys->i_title_offset += p_pgc->cell_playback[i].last_sector -
1016 p_pgc->cell_playback[i].first_sector + 1;
1019 p_sys->i_pack_len = 0;
1020 p_sys->i_next_vobu = p_sys->i_cur_block =
1021 p_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1023 if( p_demux->info.i_seekpoint != i_chapter )
1025 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1026 p_demux->info.i_seekpoint = i_chapter;
1029 else if( i_chapter != -1 )
1032 return VLC_EGENERIC; /* Couldn't set chapter */
1042 /*****************************************************************************
1043 * DvdReadSeek : Goes to a given position on the stream.
1044 *****************************************************************************
1045 * This one is used by the input and translate chronological position from
1046 * input to logical position on the device.
1047 *****************************************************************************/
1048 static void DvdReadSeek( demux_t *p_demux, int i_block_offset )
1050 demux_sys_t *p_sys = p_demux->p_sys;
1057 #define p_pgc p_sys->p_cur_pgc
1058 #define p_vts p_sys->p_vts_file
1061 i_block = i_block_offset;
1062 for( i_cell = p_sys->i_title_start_cell;
1063 i_cell <= p_sys->i_title_end_cell; i_cell++ )
1065 if( i_block < (int)p_pgc->cell_playback[i_cell].last_sector -
1066 (int)p_pgc->cell_playback[i_cell].first_sector + 1 ) break;
1068 i_block -= (p_pgc->cell_playback[i_cell].last_sector -
1069 p_pgc->cell_playback[i_cell].first_sector + 1);
1071 if( i_cell > p_sys->i_title_end_cell )
1073 msg_Err( p_demux, "couldn't find cell for block %i", i_block_offset );
1076 i_block += p_pgc->cell_playback[i_cell].first_sector;
1077 p_sys->i_title_offset = i_block_offset;
1080 for( i_chapter = 0; i_chapter < p_sys->i_chapters; i_chapter++ )
1082 int pgc_id, pgn, i_tmp;
1084 pgc_id = p_vts->vts_ptt_srpt->title[
1085 p_sys->i_ttn - 1].ptt[i_chapter].pgcn;
1086 pgn = p_vts->vts_ptt_srpt->title[
1087 p_sys->i_ttn - 1].ptt[i_chapter].pgn;
1089 i_tmp = p_vts->vts_pgcit->pgci_srp[pgc_id - 1].pgc->program_map[pgn-1];
1091 if( i_tmp > i_cell ) break;
1094 if( i_chapter < p_sys->i_chapters &&
1095 p_demux->info.i_seekpoint != i_chapter )
1097 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1098 p_demux->info.i_seekpoint = i_chapter;
1102 while( (int)p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu] <= i_block )
1108 while( p_vts->vts_c_adt->cell_adr_table[i_sub_cell].start_sector <
1109 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu-1] )
1115 msg_Dbg( p_demux, "cell %d i_sub_cell %d chapter %d vobu %d "
1116 "cell_sector %d vobu_sector %d sub_cell_sector %d",
1117 i_cell, i_sub_cell, i_chapter, i_vobu,
1118 p_sys->p_cur_pgc->cell_playback[i_cell].first_sector,
1119 p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu],
1120 p_vts->vts_c_adt->cell_adr_table[i_sub_cell - 1].start_sector);
1123 p_sys->i_cur_block = i_block;
1124 p_sys->i_next_vobu = p_vts->vts_vobu_admap->vobu_start_sectors[i_vobu];
1125 p_sys->i_pack_len = p_sys->i_next_vobu - i_block;
1126 p_sys->i_cur_cell = i_cell;
1127 p_sys->i_chapter = i_chapter;
1128 DvdReadFindCell( p_demux );
1136 /*****************************************************************************
1138 *****************************************************************************/
1139 static void DvdReadHandleDSI( demux_t *p_demux, uint8_t *p_data )
1141 demux_sys_t *p_sys = p_demux->p_sys;
1143 navRead_DSI( &p_sys->dsi_pack, &p_data[DSI_START_BYTE] );
1146 * Determine where we go next. These values are the ones we mostly
1149 p_sys->i_cur_block = p_sys->dsi_pack.dsi_gi.nv_pck_lbn;
1150 p_sys->i_pack_len = p_sys->dsi_pack.dsi_gi.vobu_ea;
1153 * Store the timecodes so we can get the current time
1155 p_sys->i_title_cur_time = (mtime_t) (p_sys->dsi_pack.dsi_gi.nv_pck_scr / 90 * 1000);
1156 p_sys->i_cell_cur_time = (mtime_t) dvdtime_to_time( &p_sys->dsi_pack.dsi_gi.c_eltm, 0 );
1159 * If we're not at the end of this cell, we can determine the next
1160 * VOBU to display using the VOBU_SRI information section of the
1161 * DSI. Using this value correctly follows the current angle,
1162 * avoiding the doubled scenes in The Matrix, and makes our life
1166 p_sys->i_next_vobu = p_sys->i_cur_block +
1167 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1169 if( p_sys->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL
1170 && p_sys->i_angle > 1 )
1172 switch( ( p_sys->dsi_pack.sml_pbi.category & 0xf000 ) >> 12 )
1175 /* Interleaved unit with no angle */
1176 if( p_sys->dsi_pack.sml_pbi.ilvu_sa != 0 )
1178 p_sys->i_next_vobu = p_sys->i_cur_block +
1179 p_sys->dsi_pack.sml_pbi.ilvu_sa;
1180 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1184 p_sys->i_next_vobu = p_sys->i_cur_block +
1185 p_sys->dsi_pack.dsi_gi.vobu_ea + 1;
1189 /* vobu is end of ilvu */
1190 if( p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address )
1192 p_sys->i_next_vobu = p_sys->i_cur_block +
1193 p_sys->dsi_pack.sml_agli.data[p_sys->i_angle-1].address;
1194 p_sys->i_pack_len = p_sys->dsi_pack.sml_pbi.ilvu_ea;
1199 /* vobu is beginning of ilvu */
1203 /* entering interleaved section */
1205 /* non interleaved cells in interleaved section */
1207 p_sys->i_next_vobu = p_sys->i_cur_block +
1208 ( p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
1212 else if( p_sys->dsi_pack.vobu_sri.next_vobu == SRI_END_OF_CELL )
1214 p_sys->i_cur_cell = p_sys->i_next_cell;
1217 if( p_sys->i_cur_cell >= p_sys->p_cur_pgc->nr_of_cells ) return;
1219 DvdReadFindCell( p_demux );
1221 p_sys->i_next_vobu =
1222 p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].first_sector;
1224 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 );
1229 msg_Dbg( p_demux, "scr %d lbn 0x%02x vobu_ea %d vob_id %d c_id %d c_time %lld",
1230 p_sys->dsi_pack.dsi_gi.nv_pck_scr,
1231 p_sys->dsi_pack.dsi_gi.nv_pck_lbn,
1232 p_sys->dsi_pack.dsi_gi.vobu_ea,
1233 p_sys->dsi_pack.dsi_gi.vobu_vob_idn,
1234 p_sys->dsi_pack.dsi_gi.vobu_c_idn,
1235 dvdtime_to_time( &p_sys->dsi_pack.dsi_gi.c_eltm, 0 ) );
1237 msg_Dbg( p_demux, "cell duration: %lld",
1238 (mtime_t)dvdtime_to_time( &p_sys->p_cur_pgc->cell_playback[p_sys->i_cur_cell].playback_time, 0 ) );
1240 msg_Dbg( p_demux, "cat 0x%02x ilvu_ea %d ilvu_sa %d size %d",
1241 p_sys->dsi_pack.sml_pbi.category,
1242 p_sys->dsi_pack.sml_pbi.ilvu_ea,
1243 p_sys->dsi_pack.sml_pbi.ilvu_sa,
1244 p_sys->dsi_pack.sml_pbi.size );
1246 msg_Dbg( p_demux, "next_vobu %d next_ilvu1 %d next_ilvu2 %d",
1247 p_sys->dsi_pack.vobu_sri.next_vobu & 0x7fffffff,
1248 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle - 1 ].address,
1249 p_sys->dsi_pack.sml_agli.data[ p_sys->i_angle ].address);
1253 /*****************************************************************************
1255 *****************************************************************************/
1256 static void DvdReadFindCell( demux_t *p_demux )
1258 demux_sys_t *p_sys = p_demux->p_sys;
1264 #define cell p_sys->p_cur_pgc->cell_playback
1266 if( cell[p_sys->i_cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1268 p_sys->i_cur_cell += p_sys->i_angle - 1;
1270 while( cell[p_sys->i_cur_cell+i].block_mode != BLOCK_MODE_LAST_CELL )
1274 p_sys->i_next_cell = p_sys->i_cur_cell + i + 1;
1278 p_sys->i_next_cell = p_sys->i_cur_cell + 1;
1283 if( p_sys->i_chapter + 1 >= p_sys->i_chapters ) return;
1285 pgc_id = p_sys->p_vts_file->vts_ptt_srpt->title[
1286 p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgcn;
1287 pgn = p_sys->p_vts_file->vts_ptt_srpt->title[
1288 p_sys->i_ttn - 1].ptt[p_sys->i_chapter + 1].pgn;
1289 p_pgc = p_sys->p_vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
1291 if( p_sys->i_cur_cell >= p_pgc->program_map[pgn - 1] - 1 )
1295 if( p_sys->i_chapter < p_sys->i_chapters &&
1296 p_demux->info.i_seekpoint != p_sys->i_chapter )
1298 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1299 p_demux->info.i_seekpoint = p_sys->i_chapter;
1304 /*****************************************************************************
1305 * DemuxTitles: get the titles/chapters structure
1306 *****************************************************************************/
1307 static void DemuxTitles( demux_t *p_demux, int *pi_angle )
1309 VLC_UNUSED( pi_angle );
1311 demux_sys_t *p_sys = p_demux->p_sys;
1315 /* Find out number of titles/chapters */
1316 #define tt_srpt p_sys->p_vmg_file->tt_srpt
1318 int32_t i_titles = tt_srpt->nr_of_srpts;
1319 msg_Dbg( p_demux, "number of titles: %d", i_titles );
1321 for( int i = 0; i < i_titles; i++ )
1323 int32_t i_chapters = 0;
1326 i_chapters = tt_srpt->title[i].nr_of_ptts;
1327 msg_Dbg( p_demux, "title %d has %d chapters", i, i_chapters );
1329 t = vlc_input_title_New();
1331 for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
1333 s = vlc_seekpoint_New();
1334 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
1337 TAB_APPEND( p_sys->i_titles, p_sys->titles, t );