1 /* $Id: dvd.c,v 1.12 2005/11/25 15:05:25 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.fr/>.
5 It may be used under the terms of the GNU General Public License. */
11 #include "dvdnav/dvdnav.h"
12 #include "dvdread/ifo_read.h"
13 #include "dvdread/ifo_print.h"
14 #include "dvdread/nav_read.h"
16 #define DVD_READ_CACHE 1
18 static char * hb_dvdnav_name( char * path );
19 static hb_dvd_t * hb_dvdnav_init( char * path );
20 static int hb_dvdnav_title_count( hb_dvd_t * d );
21 static hb_title_t * hb_dvdnav_title_scan( hb_dvd_t * d, int t );
22 static int hb_dvdnav_start( hb_dvd_t * d, hb_title_t *title, int chapter );
23 static void hb_dvdnav_stop( hb_dvd_t * d );
24 static int hb_dvdnav_seek( hb_dvd_t * d, float f );
25 static int hb_dvdnav_read( hb_dvd_t * d, hb_buffer_t * b );
26 static int hb_dvdnav_chapter( hb_dvd_t * d );
27 static void hb_dvdnav_close( hb_dvd_t ** _d );
28 static int hb_dvdnav_angle_count( hb_dvd_t * d );
29 static void hb_dvdnav_set_angle( hb_dvd_t * d, int angle );
30 static int hb_dvdnav_main_feature( hb_dvd_t * d, hb_list_t * list_title );
32 hb_dvd_func_t hb_dvdnav_func =
37 hb_dvdnav_title_count,
44 hb_dvdnav_angle_count,
46 hb_dvdnav_main_feature
49 // there can be at most 999 PGCs per title. round that up to the nearest
53 /***********************************************************************
55 **********************************************************************/
56 static void PgcWalkInit( uint32_t pgcn_map[MAX_PGCN/32] );
57 static int FindChapterIndex( hb_list_t * list, int pgcn, int pgn );
58 static int NextPgcn( ifo_handle_t *ifo, int pgcn, uint32_t pgcn_map[MAX_PGCN/32] );
59 static int FindNextCell( pgc_t *pgc, int cell_cur );
60 static int dvdtime2msec( dvd_time_t * );
62 hb_dvd_func_t * hb_dvdnav_methods( void )
64 return &hb_dvdnav_func;
67 static char * hb_dvdnav_name( char * path )
69 static char name[1024];
70 unsigned char unused[1024];
71 dvd_reader_t * reader;
73 reader = DVDOpen( path );
79 if( DVDUDFVolumeInfo( reader, name, sizeof( name ),
80 unused, sizeof( unused ) ) )
90 /***********************************************************************
92 ***********************************************************************
93 * Once dvdnav has entered the 'stopped' state, it can not be revived
94 * dvdnav_reset doesn't work because it doesn't remember the path
95 * So this function re-opens dvdnav
96 **********************************************************************/
97 static int hb_dvdnav_reset( hb_dvdnav_t * d )
100 dvdnav_close( d->dvdnav );
103 if( dvdnav_open(&d->dvdnav, d->path) != DVDNAV_STATUS_OK )
106 * Not an error, may be a stream - which we'll try in a moment.
108 hb_log( "dvd: not a dvd - trying as a stream/file instead" );
112 if (dvdnav_set_readahead_flag(d->dvdnav, DVD_READ_CACHE) !=
115 hb_error("Error: dvdnav_set_readahead_flag: %s\n",
116 dvdnav_err_to_string(d->dvdnav));
121 ** set the PGC positioning flag to have position information
122 ** relatively to the whole feature instead of just relatively to the
125 if (dvdnav_set_PGC_positioning_flag(d->dvdnav, 1) != DVDNAV_STATUS_OK)
127 hb_error("Error: dvdnav_set_PGC_positioning_flag: %s\n",
128 dvdnav_err_to_string(d->dvdnav));
134 if( d->dvdnav ) dvdnav_close( d->dvdnav );
138 /***********************************************************************
140 ***********************************************************************
142 **********************************************************************/
143 static hb_dvd_t * hb_dvdnav_init( char * path )
149 e = calloc( sizeof( hb_dvd_t ), 1 );
152 /* Log DVD drive region code */
153 if ( hb_dvd_region( path, ®ion_mask ) == 0 )
155 hb_log( "dvd: Region mask 0x%02x", region_mask );
156 if ( region_mask == 0xFF )
158 hb_log( "dvd: Warning, DVD device has no region set" );
163 if( dvdnav_open(&d->dvdnav, path) != DVDNAV_STATUS_OK )
166 * Not an error, may be a stream - which we'll try in a moment.
168 hb_log( "dvd: not a dvd - trying as a stream/file instead" );
172 if (dvdnav_set_readahead_flag(d->dvdnav, DVD_READ_CACHE) !=
175 hb_error("Error: dvdnav_set_readahead_flag: %s\n",
176 dvdnav_err_to_string(d->dvdnav));
181 ** set the PGC positioning flag to have position information
182 ** relatively to the whole feature instead of just relatively to the
185 if (dvdnav_set_PGC_positioning_flag(d->dvdnav, 1) != DVDNAV_STATUS_OK)
187 hb_error("Error: dvdnav_set_PGC_positioning_flag: %s\n",
188 dvdnav_err_to_string(d->dvdnav));
193 if( !( d->reader = DVDOpen( path ) ) )
196 * Not an error, may be a stream - which we'll try in a moment.
198 hb_log( "dvd: not a dvd - trying as a stream/file instead" );
203 if( !( d->vmg = ifoOpen( d->reader, 0 ) ) )
205 hb_error( "dvd: ifoOpen failed" );
209 d->path = strdup( path );
214 if( d->dvdnav ) dvdnav_close( d->dvdnav );
215 if( d->vmg ) ifoClose( d->vmg );
216 if( d->reader ) DVDClose( d->reader );
221 /***********************************************************************
222 * hb_dvdnav_title_count
223 **********************************************************************/
224 static int hb_dvdnav_title_count( hb_dvd_t * e )
227 hb_dvdnav_t * d = &(e->dvdnav);
229 dvdnav_get_number_of_titles(d->dvdnav, &titles);
234 PttDuration(ifo_handle_t *ifo, int ttn, int pttn, int *blocks, int *last_pgcn)
238 uint64_t duration = 0;
239 int cell_start, cell_end;
242 // Initialize map of visited pgc's to prevent loops
243 uint32_t pgcn_map[MAX_PGCN/32];
244 PgcWalkInit( pgcn_map );
245 pgcn = ifo->vts_ptt_srpt->title[ttn-1].ptt[pttn-1].pgcn;
246 pgn = ifo->vts_ptt_srpt->title[ttn-1].ptt[pttn-1].pgn;
247 if ( pgcn < 1 || pgcn > ifo->vts_pgcit->nr_of_pgci_srp || pgcn >= MAX_PGCN)
249 hb_error( "invalid PGC ID %d, skipping", pgcn );
253 if( pgn <= 0 || pgn > 99 )
255 hb_error( "scan: pgn %d not valid, skipping", pgn );
262 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
265 hb_error( "scan: pgc not valid, skipping" );
268 if (pgn > pgc->nr_of_programs)
274 duration += 90LL * dvdtime2msec( &pgc->playback_time );
276 cell_start = pgc->program_map[pgn-1] - 1;
277 cell_end = pgc->nr_of_cells - 1;
278 for(i = cell_start; i <= cell_end; i = FindNextCell(pgc, i))
280 *blocks += pgc->cell_playback[i].last_sector + 1 -
281 pgc->cell_playback[i].first_sector;
285 } while((pgcn = NextPgcn(ifo, pgcn, pgcn_map)) != 0);
289 /***********************************************************************
290 * hb_dvdnav_title_scan
291 **********************************************************************/
292 static hb_title_t * hb_dvdnav_title_scan( hb_dvd_t * e, int t )
295 hb_dvdnav_t * d = &(e->dvdnav);
297 ifo_handle_t * ifo = NULL;
298 int pgcn, pgn, pgcn_end, i, c;
302 hb_chapter_t * chapter;
304 uint64_t duration, longest;
305 int longest_pgcn, longest_pgn, longest_pgcn_end;
306 float duration_correction;
309 hb_log( "scan: scanning title %d", t );
311 title = hb_title_init( d->path, t );
312 title->type = HB_DVD_TYPE;
313 if (dvdnav_get_title_string(d->dvdnav, &name) == DVDNAV_STATUS_OK)
315 strncpy( title->name, name, sizeof( title->name ) );
319 char * p_cur, * p_last = d->path;
320 for( p_cur = d->path; *p_cur; p_cur++ )
322 if( p_cur[0] == '/' && p_cur[1] )
327 snprintf( title->name, sizeof( title->name ), "%s", p_last );
330 /* VTS which our title is in */
331 title->vts = d->vmg->tt_srpt->title[t-1].title_set_nr;
335 /* A VTS of 0 means the title wasn't found in the title set */
336 hb_error("Invalid VTS (title set) number: %i", title->vts);
340 hb_log( "scan: opening IFO for VTS %d", title->vts );
341 if( !( ifo = ifoOpen( d->reader, title->vts ) ) )
343 hb_error( "scan: ifoOpen failed" );
347 /* ignore titles with bogus cell addresses so we don't abort later
349 for ( i = 0; i < ifo->vts_c_adt->nr_of_vobs; ++i)
351 if( (ifo->vts_c_adt->cell_adr_table[i].start_sector & 0xffffff ) ==
354 hb_error( "scan: cell_adr_table[%d].start_sector invalid (0x%x) "
355 "- skipping title", i,
356 ifo->vts_c_adt->cell_adr_table[i].start_sector );
359 if( (ifo->vts_c_adt->cell_adr_table[i].last_sector & 0xffffff ) ==
362 hb_error( "scan: cell_adr_table[%d].last_sector invalid (0x%x) "
363 "- skipping title", i,
364 ifo->vts_c_adt->cell_adr_table[i].last_sector );
367 if( ifo->vts_c_adt->cell_adr_table[i].start_sector >=
368 ifo->vts_c_adt->cell_adr_table[i].last_sector )
370 hb_error( "scan: cell_adr_table[%d].start_sector (0x%x) "
371 "is not before last_sector (0x%x) - skipping title", i,
372 ifo->vts_c_adt->cell_adr_table[i].start_sector,
373 ifo->vts_c_adt->cell_adr_table[i].last_sector );
378 if( global_verbosity_level == 3 )
380 ifo_print( d->reader, title->vts );
383 /* Position of the title in the VTS */
384 title->ttn = d->vmg->tt_srpt->title[t-1].vts_ttn;
385 if ( title->ttn < 1 || title->ttn > ifo->vts_ptt_srpt->nr_of_srpts )
387 hb_error( "invalid VTS PTT offset %d for title %d, skipping", title->ttn, t );
394 longest_pgcn_end = -1;
396 for( i = 0; i < ifo->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts; i++ )
400 duration = PttDuration(ifo, title->ttn, i+1, &blocks, &pgcn_end);
401 pgcn = ifo->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgcn;
402 pgn = ifo->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgn;
403 if( duration > longest )
407 longest_pgcn_end = pgcn_end;
409 title->block_count = blocks;
411 else if (pgcn == longest_pgcn && pgn < longest_pgn)
414 title->block_count = blocks;
418 /* ignore titles under 10 seconds because they're often stills or
419 * clips with no audio & our preview code doesn't currently handle
420 * either of these. */
421 if( longest < 900000LL )
423 hb_log( "scan: ignoring title (too short)" );
428 pgcn_end = longest_pgcn_end;
434 if ( pgcn < 1 || pgcn > ifo->vts_pgcit->nr_of_pgci_srp || pgcn >= MAX_PGCN)
436 hb_error( "invalid PGC ID %d for title %d, skipping", pgcn, t );
440 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
442 hb_log("pgc_id: %d, pgn: %d: pgc: %p", pgcn, pgn, pgc);
443 if (pgn > pgc->nr_of_programs)
445 hb_error( "invalid PGN %d for title %d, skipping", pgn, t );
450 title->cell_start = pgc->program_map[pgn-1] - 1;
451 title->block_start = pgc->cell_playback[title->cell_start].first_sector;
453 pgc = ifo->vts_pgcit->pgci_srp[pgcn_end-1].pgc;
456 title->cell_end = pgc->nr_of_cells - 1;
457 title->block_end = pgc->cell_playback[title->cell_end].last_sector;
459 hb_log( "scan: vts=%d, ttn=%d, cells=%d->%d, blocks=%d->%d, "
460 "%d blocks", title->vts, title->ttn, title->cell_start,
461 title->cell_end, title->block_start, title->block_end,
462 title->block_count );
465 title->duration = longest;
466 title->hours = title->duration / 90000 / 3600;
467 title->minutes = ( ( title->duration / 90000 ) % 3600 ) / 60;
468 title->seconds = ( title->duration / 90000 ) % 60;
469 hb_log( "scan: duration is %02d:%02d:%02d (%"PRId64" ms)",
470 title->hours, title->minutes, title->seconds,
471 title->duration / 90 );
473 /* Detect languages */
474 for( i = 0; i < ifo->vtsi_mat->nr_of_vts_audio_streams; i++ )
476 hb_audio_t * audio, * audio_tmp;
477 int audio_format, lang_code, audio_control,
479 iso639_lang_t * lang;
480 int lang_extension = 0;
482 hb_log( "scan: checking audio %d", i + 1 );
484 audio = calloc( sizeof( hb_audio_t ), 1 );
486 audio_format = ifo->vtsi_mat->vts_audio_attr[i].audio_format;
487 lang_code = ifo->vtsi_mat->vts_audio_attr[i].lang_code;
488 lang_extension = ifo->vtsi_mat->vts_audio_attr[i].code_extension;
490 ifo->vts_pgcit->pgci_srp[title_pgcn-1].pgc->audio_control[i];
492 if( !( audio_control & 0x8000 ) )
494 hb_log( "scan: audio channel is not active" );
499 position = ( audio_control & 0x7F00 ) >> 8;
501 switch( audio_format )
504 audio->id = ( ( 0x80 + position ) << 8 ) | 0xbd;
505 audio->config.in.codec = HB_ACODEC_AC3;
510 audio->id = 0xc0 + position;
511 audio->config.in.codec = HB_ACODEC_MPGA;
515 audio->id = ( ( 0xa0 + position ) << 8 ) | 0xbd;
516 audio->config.in.codec = HB_ACODEC_LPCM;
520 audio->id = ( ( 0x88 + position ) << 8 ) | 0xbd;
521 audio->config.in.codec = HB_ACODEC_DCA;
526 audio->config.in.codec = 0;
527 hb_log( "scan: unknown audio codec (%x)",
536 /* Check for duplicate tracks */
538 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
540 audio_tmp = hb_list_item( title->list_audio, j );
541 if( audio->id == audio_tmp->id )
549 hb_log( "scan: duplicate audio track" );
554 audio->config.lang.type = lang_extension;
556 lang = lang_for_code( ifo->vtsi_mat->vts_audio_attr[i].lang_code );
558 snprintf( audio->config.lang.description, sizeof( audio->config.lang.description ), "%s (%s)",
559 strlen(lang->native_name) ? lang->native_name : lang->eng_name,
560 audio->config.in.codec == HB_ACODEC_AC3 ? "AC3" : ( audio->config.in.codec ==
561 HB_ACODEC_DCA ? "DTS" : ( audio->config.in.codec ==
562 HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) ) );
563 snprintf( audio->config.lang.simple, sizeof( audio->config.lang.simple ), "%s",
564 strlen(lang->native_name) ? lang->native_name : lang->eng_name );
565 snprintf( audio->config.lang.iso639_2, sizeof( audio->config.lang.iso639_2 ), "%s",
568 switch( lang_extension )
574 strcat( audio->config.lang.description, " (Visually Impaired)" );
577 strcat( audio->config.lang.description, " (Director's Commentary 1)" );
580 strcat( audio->config.lang.description, " (Director's Commentary 2)" );
586 hb_log( "scan: id=%x, lang=%s, 3cc=%s ext=%i", audio->id,
587 audio->config.lang.description, audio->config.lang.iso639_2,
590 audio->config.in.track = i;
591 hb_list_add( title->list_audio, audio );
594 memcpy( title->palette,
595 ifo->vts_pgcit->pgci_srp[title_pgcn-1].pgc->palette,
596 16 * sizeof( uint32_t ) );
598 /* Check for subtitles */
599 for( i = 0; i < ifo->vtsi_mat->nr_of_vts_subp_streams; i++ )
601 hb_subtitle_t * subtitle;
604 iso639_lang_t * lang;
605 int lang_extension = 0;
607 hb_log( "scan: checking subtitle %d", i + 1 );
610 ifo->vts_pgcit->pgci_srp[title_pgcn-1].pgc->subp_control[i];
612 if( !( spu_control & 0x80000000 ) )
614 hb_log( "scan: subtitle channel is not active" );
618 if( ifo->vtsi_mat->vts_video_attr.display_aspect_ratio )
620 switch( ifo->vtsi_mat->vts_video_attr.permitted_df )
623 position = spu_control & 0xFF;
626 position = ( spu_control >> 8 ) & 0xFF;
629 position = ( spu_control >> 16 ) & 0xFF;
634 position = ( spu_control >> 24 ) & 0x7F;
637 lang_extension = ifo->vtsi_mat->vts_subp_attr[i].code_extension;
639 lang = lang_for_code( ifo->vtsi_mat->vts_subp_attr[i].lang_code );
641 subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
642 subtitle->track = i+1;
643 subtitle->id = ( ( 0x20 + position ) << 8 ) | 0xbd;
644 snprintf( subtitle->lang, sizeof( subtitle->lang ), "%s",
645 strlen(lang->native_name) ? lang->native_name : lang->eng_name);
646 snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ), "%s",
648 subtitle->format = PICTURESUB;
649 subtitle->source = VOBSUB;
650 subtitle->config.dest = RENDERSUB; // By default render (burn-in) the VOBSUB.
652 subtitle->type = lang_extension;
654 switch( lang_extension )
661 strcat( subtitle->lang, " (Caption with bigger size character)");
664 strcat( subtitle->lang, " (Caption for Children)");
669 strcat( subtitle->lang, " (Closed Caption)");
672 strcat( subtitle->lang, " (Closed Caption with bigger size character)");
675 strcat( subtitle->lang, " (Closed Caption for Children)");
680 strcat( subtitle->lang, " (Forced Caption)");
689 strcat( subtitle->lang, " (Director's Commentary)");
692 strcat( subtitle->lang, " (Director's Commentary with bigger size character)");
695 strcat( subtitle->lang, " (Director's Commentary for Children)");
700 hb_log( "scan: id=%x, lang=%s, 3cc=%s", subtitle->id,
701 subtitle->lang, subtitle->iso639_2 );
703 hb_list_add( title->list_subtitle, subtitle );
707 uint32_t pgcn_map[MAX_PGCN/32];
708 PgcWalkInit( pgcn_map );
712 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
714 for (i = pgn; i <= pgc->nr_of_programs; i++)
716 chapter = calloc( sizeof( hb_chapter_t ), 1 );
718 chapter->index = c + 1;
719 chapter->pgcn = pgcn;
721 hb_list_add( title->list_chapter, chapter );
726 } while ((pgcn = NextPgcn(ifo, pgcn, pgcn_map)) != 0);
728 hb_log( "scan: title %d has %d chapters", t, c );
731 count = hb_list_count( title->list_chapter );
732 for (i = 0; i < count; i++)
734 chapter = hb_list_item( title->list_chapter, i );
736 pgcn = chapter->pgcn;
738 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
741 chapter->cell_start = pgc->program_map[pgn-1] - 1;
742 chapter->block_start = pgc->cell_playback[chapter->cell_start].first_sector;
743 // if there are no more programs in this pgc, the end cell is the
744 // last cell. Otherwise it's the cell before the start cell of the
746 if ( pgn == pgc->nr_of_programs )
748 chapter->cell_end = pgc->nr_of_cells - 1;
752 chapter->cell_end = pgc->program_map[pgn] - 2;;
754 chapter->block_end = pgc->cell_playback[chapter->cell_end].last_sector;
756 /* Block count, duration */
757 chapter->block_count = 0;
758 chapter->duration = 0;
760 cell_cur = chapter->cell_start;
761 while( cell_cur <= chapter->cell_end )
763 #define cp pgc->cell_playback[cell_cur]
764 chapter->block_count += cp.last_sector + 1 - cp.first_sector;
765 chapter->duration += 90LL * dvdtime2msec( &cp.playback_time );
767 cell_cur = FindNextCell( pgc, cell_cur );
769 duration += chapter->duration;
772 /* The durations we get for chapters aren't precise. Scale them so
773 the total matches the title duration */
774 duration_correction = (float) title->duration / (float) duration;
775 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
778 chapter = hb_list_item( title->list_chapter, i );
779 chapter->duration = duration_correction * chapter->duration;
780 seconds = ( chapter->duration + 45000 ) / 90000;
781 chapter->hours = seconds / 3600;
782 chapter->minutes = ( seconds % 3600 ) / 60;
783 chapter->seconds = seconds % 60;
785 hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %"PRId64" ms",
786 chapter->index, chapter->cell_start, chapter->cell_end,
787 chapter->block_start, chapter->block_end,
788 chapter->block_count, chapter->duration / 90 );
791 /* Get aspect. We don't get width/height/rate infos here as
792 they tend to be wrong */
793 switch( ifo->vtsi_mat->vts_video_attr.display_aspect_ratio )
796 title->container_aspect = 4. / 3.;
799 title->container_aspect = 16. / 9.;
802 hb_log( "scan: unknown aspect" );
806 hb_log( "scan: aspect = %g", title->aspect );
808 /* This title is ok so far */
812 hb_list_close( &title->list_audio );
817 if( ifo ) ifoClose( ifo );
822 /***********************************************************************
823 * hb_dvdnav_title_scan
824 **********************************************************************/
825 static int find_title( hb_list_t * list_title, int title )
829 for ( ii = 0; ii < hb_list_count( list_title ); ii++ )
831 hb_title_t * hbtitle = hb_list_item( list_title, ii );
832 if ( hbtitle->index == title )
838 static int try_button( dvdnav_t * dvdnav, int menu_id, int button, hb_list_t * list_title )
840 int result, event, len;
841 uint8_t buf[HB_DVD_READ_BUFFER_SIZE];
843 int32_t cur_title, title, pgcn, pgn;
844 uint64_t longest_duration = 0;
847 pci_t *pci = dvdnav_get_current_nav_pci( dvdnav );
848 result = dvdnav_button_select_and_activate( dvdnav, pci, button + 1 );
849 if (result != DVDNAV_STATUS_OK)
851 hb_log("dvdnav_button_select_and_activate: %s", dvdnav_err_to_string(dvdnav));
854 result = dvdnav_current_title_program( dvdnav, &title, &pgcn, &pgn );
855 if (result != DVDNAV_STATUS_OK)
856 hb_log("dvdnav cur pgcn err: %s", dvdnav_err_to_string(dvdnav));
860 hb_title_t * hbtitle;
862 index = find_title( list_title, title );
863 hbtitle = hb_list_item( list_title, index );
864 if ( hbtitle != NULL )
866 if ( hbtitle->duration > longest_duration )
868 longest_duration = hbtitle->duration;
872 // If the duration is longer than 10min, assume
873 // this isn't garbage leading to something bigger.
874 if ( longest_duration / 90000 > 10 * 60 )
881 for (ii = 0; ii < 8000; ii++)
883 result = dvdnav_get_next_block( dvdnav, buf, &event, &len );
884 if ( result == DVDNAV_STATUS_ERR )
886 hb_error("dvdnav: Read Error, %s", dvdnav_err_to_string(dvdnav));
891 case DVDNAV_BLOCK_OK:
894 case DVDNAV_CELL_CHANGE:
896 result = dvdnav_current_title_program( dvdnav, &title, &pgcn, &pgn );
897 if (result != DVDNAV_STATUS_OK)
898 hb_log("dvdnav cur pgcn err: %s", dvdnav_err_to_string(dvdnav));
899 if ( title != cur_title && title > 0 )
901 hb_title_t * hbtitle;
903 index = find_title( list_title, title );
904 hbtitle = hb_list_item( list_title, index );
905 if ( hbtitle != NULL )
907 if ( hbtitle->duration > longest_duration )
909 longest_duration = hbtitle->duration;
913 // If the duration is longer than 10min, assume
914 // this isn't garbage leading to something bigger.
915 if ( longest_duration / 90000 > 10 * 60 )
923 result = dvdnav_next_pg_search( dvdnav );
924 if ( result != DVDNAV_STATUS_OK )
928 int next_title, next_pgcn, next_pgn;
929 dvdnav_current_title_program( dvdnav, &next_title, &next_pgcn, &next_pgn );
930 if (title == next_title && pgcn == next_pgcn && pgn == next_pgn)
937 case DVDNAV_STILL_FRAME:
938 dvdnav_still_skip( dvdnav );
942 dvdnav_wait_skip( dvdnav );
946 hb_log("dvdnav: stop encountered during seek");
949 case DVDNAV_HOP_CHANNEL:
952 case DVDNAV_NAV_PACKET:
956 case DVDNAV_VTS_CHANGE:
958 result = dvdnav_current_title_program( dvdnav, &title, &pgcn, &pgn );
959 if (result != DVDNAV_STATUS_OK)
960 hb_log("dvdnav cur pgcn err: %s", dvdnav_err_to_string(dvdnav));
961 if ( title != cur_title && title > 0 )
963 hb_title_t * hbtitle;
965 index = find_title( list_title, title );
966 hbtitle = hb_list_item( list_title, index );
967 if ( hbtitle != NULL )
969 if ( hbtitle->duration > longest_duration )
971 longest_duration = hbtitle->duration;
976 if ( longest_duration / 90000 > 10 * 60 )
983 dvdnav_next_pg_search( dvdnav );
987 case DVDNAV_HIGHLIGHT:
990 case DVDNAV_AUDIO_STREAM_CHANGE:
993 case DVDNAV_SPU_STREAM_CHANGE:
996 case DVDNAV_SPU_CLUT_CHANGE:
1009 static int try_menu( hb_dvdnav_t * d, hb_list_t * list_title, DVDMenuID_t menu )
1011 int result, event, len;
1012 uint8_t buf[HB_DVD_READ_BUFFER_SIZE];
1014 int32_t cur_title, title, pgcn, pgn;
1015 uint64_t longest_duration = 0;
1018 dvdnav_reset( d->dvdnav );
1019 result = dvdnav_title_play( d->dvdnav, 1 );
1020 if ( result == DVDNAV_STATUS_ERR )
1022 hb_error("dvdnav: Can not set title, %s", dvdnav_err_to_string(d->dvdnav));
1025 result = dvdnav_menu_call( d->dvdnav, menu );
1026 if ( result != DVDNAV_STATUS_OK )
1028 hb_error("dvdnav: Can not set dvd menu, %s", dvdnav_err_to_string(d->dvdnav));
1032 result = dvdnav_current_title_program( d->dvdnav, &cur_title, &pgcn, &pgn );
1033 if (result != DVDNAV_STATUS_OK)
1034 hb_log("dvdnav cur pgcn err: %s", dvdnav_err_to_string(d->dvdnav));
1036 // Hit the root menu again to see if we can force
1037 // our way to the main menu.
1038 dvdnav_next_pg_search( d->dvdnav );
1039 result = dvdnav_menu_call( d->dvdnav, menu );
1041 dvdnav_next_pg_search( d->dvdnav );
1042 result = dvdnav_menu_call( d->dvdnav, menu );
1044 if ( cur_title > 0 )
1046 dvdnav_next_pg_search( d->dvdnav );
1049 for (ii = 0; ii < 5000; ii++)
1051 result = dvdnav_get_next_block( d->dvdnav, buf, &event, &len );
1052 if ( result == DVDNAV_STATUS_ERR )
1054 hb_error("dvdnav: Read Error, %s", dvdnav_err_to_string(d->dvdnav));
1059 case DVDNAV_BLOCK_OK:
1062 case DVDNAV_CELL_CHANGE:
1064 result = dvdnav_current_title_program( d->dvdnav, &title, &pgcn, &pgn );
1065 if (result != DVDNAV_STATUS_OK)
1066 hb_log("dvdnav cur pgcn err: %s", dvdnav_err_to_string(d->dvdnav));
1070 dvdnav_next_pg_search( d->dvdnav );
1074 case DVDNAV_STILL_FRAME:
1075 dvdnav_still_skip( d->dvdnav );
1079 dvdnav_wait_skip( d->dvdnav );
1083 hb_log("dvdnav: stop encountered during seek");
1087 case DVDNAV_HOP_CHANNEL:
1090 case DVDNAV_NAV_PACKET:
1092 pci_t *pci = dvdnav_get_current_nav_pci( d->dvdnav );
1095 if ( pci == NULL ) break;
1097 buttons = pci->hli.hl_gi.btn_ns;
1098 if ( cur_title == 0 && buttons > 0 )
1100 int menu_title, menu_id;
1101 result = dvdnav_current_title_info( d->dvdnav, &menu_title, &menu_id );
1102 if (result != DVDNAV_STATUS_OK)
1103 hb_log("dvdnav cur pgcn err: %s", dvdnav_err_to_string(d->dvdnav));
1104 for (kk = 0; kk < buttons; kk++)
1106 dvdnav_t *dvdnav_copy;
1108 result = dvdnav_dup( &dvdnav_copy, d->dvdnav );
1109 if (result != DVDNAV_STATUS_OK)
1111 hb_log("dvdnav dup failed: %s", dvdnav_err_to_string(d->dvdnav));
1114 title = try_button( dvdnav_copy, menu_id, kk, list_title );
1115 dvdnav_free_dup( dvdnav_copy );
1119 hb_title_t * hbtitle;
1121 index = find_title( list_title, title );
1122 hbtitle = hb_list_item( list_title, index );
1123 if ( hbtitle != NULL )
1125 if ( hbtitle->duration > longest_duration )
1127 longest_duration = hbtitle->duration;
1137 case DVDNAV_VTS_CHANGE:
1139 result = dvdnav_current_title_program( d->dvdnav, &title, &pgcn, &pgn );
1140 if (result != DVDNAV_STATUS_OK)
1141 hb_log("dvdnav cur pgcn err: %s", dvdnav_err_to_string(d->dvdnav));
1145 dvdnav_next_pg_search( d->dvdnav );
1149 case DVDNAV_HIGHLIGHT:
1152 case DVDNAV_AUDIO_STREAM_CHANGE:
1155 case DVDNAV_SPU_STREAM_CHANGE:
1158 case DVDNAV_SPU_CLUT_CHANGE:
1173 static int hb_dvdnav_main_feature( hb_dvd_t * e, hb_list_t * list_title )
1175 hb_dvdnav_t * d = &(e->dvdnav);
1178 int longest_fallback;
1180 uint64_t longest_duration_root = 0;
1181 uint64_t longest_duration_title = 0;
1182 uint64_t longest_duration_fallback = 0;
1184 for ( ii = 0; ii < hb_list_count( list_title ); ii++ )
1186 hb_title_t * title = hb_list_item( list_title, ii );
1187 if ( title->duration > longest_duration_fallback )
1189 longest_duration_fallback = title->duration;
1190 longest_fallback = title->index;
1194 longest_root = try_menu( d, list_title, DVD_MENU_Root );
1195 if ( longest_root >= 0 )
1197 hb_title_t * hbtitle;
1199 index = find_title( list_title, longest_root );
1200 hbtitle = hb_list_item( list_title, index );
1202 longest_duration_root = hbtitle->duration;
1204 if ( longest_root < 0 ||
1205 (float)longest_duration_fallback * 0.7 > longest_duration_root)
1207 longest_title = try_menu( d, list_title, DVD_MENU_Title );
1208 if ( longest_title >= 0 )
1210 hb_title_t * hbtitle;
1212 index = find_title( list_title, longest_title );
1213 hbtitle = hb_list_item( list_title, index );
1215 longest_duration_title = hbtitle->duration;
1219 uint64_t longest_duration;
1222 if ( longest_duration_root > longest_duration_title )
1224 longest_duration = longest_duration_root;
1225 longest = longest_root;
1229 longest_duration = longest_duration_title;
1230 longest = longest_title;
1232 if ((float)longest_duration_fallback * 0.7 > longest_duration)
1234 longest = longest_fallback;
1239 /***********************************************************************
1241 ***********************************************************************
1242 * Title and chapter start at 1
1243 **********************************************************************/
1244 static int hb_dvdnav_start( hb_dvd_t * e, hb_title_t *title, int c )
1246 hb_dvdnav_t * d = &(e->dvdnav);
1247 int t = title->index;
1248 hb_chapter_t *chapter;
1249 dvdnav_status_t result;
1251 d->title_block_count = title->block_count;
1252 d->list_chapter = title->list_chapter;
1254 if ( d->stopped && !hb_dvdnav_reset(d) )
1258 dvdnav_reset( d->dvdnav );
1259 chapter = hb_list_item( title->list_chapter, c - 1);
1260 if (chapter != NULL)
1261 result = dvdnav_program_play(d->dvdnav, t, chapter->pgcn, chapter->pgn);
1263 result = dvdnav_part_play(d->dvdnav, t, 1);
1264 if (result != DVDNAV_STATUS_OK)
1266 hb_error( "dvd: dvdnav_*_play failed - %s",
1267 dvdnav_err_to_string(d->dvdnav) );
1276 /***********************************************************************
1278 ***********************************************************************
1280 **********************************************************************/
1281 static void hb_dvdnav_stop( hb_dvd_t * e )
1285 /***********************************************************************
1287 ***********************************************************************
1289 **********************************************************************/
1290 static int hb_dvdnav_seek( hb_dvd_t * e, float f )
1292 hb_dvdnav_t * d = &(e->dvdnav);
1293 uint64_t sector = f * d->title_block_count;
1294 int result, event, len;
1295 uint8_t buf[HB_DVD_READ_BUFFER_SIZE];
1303 // XXX the current version of libdvdnav can't seek outside the current
1304 // PGC. Check if the place we're seeking to is in a different
1305 // PGC. Position there & adjust the offset if so.
1306 uint64_t pgc_offset = 0;
1307 uint64_t chap_offset = 0;
1308 hb_chapter_t *pgc_change = hb_list_item(d->list_chapter, 0 );
1309 for ( ii = 0; ii < hb_list_count( d->list_chapter ); ++ii )
1311 hb_chapter_t *chapter = hb_list_item( d->list_chapter, ii );
1312 uint64_t chap_len = chapter->block_end - chapter->block_start + 1;
1314 if ( chapter->pgcn != pgc_change->pgcn )
1316 // this chapter's in a different pgc from the previous - note the
1317 // change so we can make sector offset's be pgc relative.
1318 pgc_offset = chap_offset;
1319 pgc_change = chapter;
1321 if ( chap_offset <= sector && sector < chap_offset + chap_len )
1323 // this chapter contains the sector we want - see if it's in a
1324 // different pgc than the one we're currently in.
1325 int32_t title, pgcn, pgn;
1326 if (dvdnav_current_title_program( d->dvdnav, &title, &pgcn, &pgn ) != DVDNAV_STATUS_OK)
1327 hb_log("dvdnav cur pgcn err: %s", dvdnav_err_to_string(d->dvdnav));
1328 // If we find ourselves in a new title, it means a title
1329 // transition was made while reading data. Jumping between
1330 // titles can cause the vm to get into a bad state. So
1331 // reset the vm in this case.
1332 if ( d->title != title )
1333 dvdnav_reset( d->dvdnav );
1335 if ( d->title != title || chapter->pgcn != pgcn )
1337 // this chapter is in a different pgc - switch to it.
1338 if (dvdnav_program_play(d->dvdnav, d->title, chapter->pgcn, chapter->pgn) != DVDNAV_STATUS_OK)
1339 hb_log("dvdnav prog play err: %s", dvdnav_err_to_string(d->dvdnav));
1341 // seek sectors are pgc-relative so remove the pgc start sector.
1342 sector -= pgc_offset;
1345 chap_offset += chap_len;
1348 // dvdnav will not let you seek or poll current position
1349 // till it reaches a certain point in parsing. so we
1350 // have to get blocks until we reach a cell
1351 // Put an arbitrary limit of 100 blocks on how long we search
1352 for (ii = 0; ii < 100 && !done; ii++)
1354 result = dvdnav_get_next_block( d->dvdnav, buf, &event, &len );
1355 if ( result == DVDNAV_STATUS_ERR )
1357 hb_error("dvdnav: Read Error, %s", dvdnav_err_to_string(d->dvdnav));
1362 case DVDNAV_BLOCK_OK:
1363 case DVDNAV_CELL_CHANGE:
1367 case DVDNAV_STILL_FRAME:
1368 dvdnav_still_skip( d->dvdnav );
1372 dvdnav_wait_skip( d->dvdnav );
1376 hb_log("dvdnav: stop encountered during seek");
1380 case DVDNAV_HOP_CHANNEL:
1381 case DVDNAV_NAV_PACKET:
1382 case DVDNAV_VTS_CHANGE:
1383 case DVDNAV_HIGHLIGHT:
1384 case DVDNAV_AUDIO_STREAM_CHANGE:
1385 case DVDNAV_SPU_STREAM_CHANGE:
1386 case DVDNAV_SPU_CLUT_CHANGE:
1393 if (dvdnav_sector_search(d->dvdnav, sector, SEEK_SET) != DVDNAV_STATUS_OK)
1395 hb_error( "dvd: dvdnav_sector_search failed - %s",
1396 dvdnav_err_to_string(d->dvdnav) );
1402 /***********************************************************************
1404 ***********************************************************************
1406 **********************************************************************/
1407 static int hb_dvdnav_read( hb_dvd_t * e, hb_buffer_t * b )
1409 hb_dvdnav_t * d = &(e->dvdnav);
1410 int result, event, len;
1412 int error_count = 0;
1420 result = dvdnav_get_next_block( d->dvdnav, b->data, &event, &len );
1421 if ( result == DVDNAV_STATUS_ERR )
1423 hb_error("dvdnav: Read Error, %s", dvdnav_err_to_string(d->dvdnav));
1424 if (dvdnav_sector_search(d->dvdnav, 1, SEEK_CUR) != DVDNAV_STATUS_OK)
1426 hb_error( "dvd: dvdnav_sector_search failed - %s",
1427 dvdnav_err_to_string(d->dvdnav) );
1431 if (error_count > 10)
1433 hb_error("dvdnav: Error, too many consecutive read errors");
1440 case DVDNAV_BLOCK_OK:
1441 // We have received a regular block of the currently playing
1444 // The muxers expect to only get chapter 2 and above
1445 // They write chapter 1 when chapter 2 is detected.
1447 b->new_chap = chapter;
1454 * Nothing to do here.
1458 case DVDNAV_STILL_FRAME:
1460 * We have reached a still frame. A real player application
1461 * would wait the amount of time specified by the still's
1462 * length while still handling user input to make menus and
1463 * other interactive stills work. A length of 0xff means an
1464 * indefinite still which has to be skipped indirectly by some
1467 dvdnav_still_skip( d->dvdnav );
1472 * We have reached a point in DVD playback, where timing is
1473 * critical. Player application with internal fifos can
1474 * introduce state inconsistencies, because libdvdnav is
1475 * always the fifo's length ahead in the stream compared to
1476 * what the application sees. Such applications should wait
1477 * until their fifos are empty when they receive this type of
1480 dvdnav_wait_skip( d->dvdnav );
1483 case DVDNAV_SPU_CLUT_CHANGE:
1485 * Player applications should pass the new colour lookup table
1486 * to their SPU decoder
1490 case DVDNAV_SPU_STREAM_CHANGE:
1492 * Player applications should inform their SPU decoder to
1497 case DVDNAV_AUDIO_STREAM_CHANGE:
1499 * Player applications should inform their audio decoder to
1504 case DVDNAV_HIGHLIGHT:
1506 * Player applications should inform their overlay engine to
1507 * highlight the given button
1511 case DVDNAV_VTS_CHANGE:
1513 * Some status information like video aspect and video scale
1514 * permissions do not change inside a VTS. Therefore this
1515 * event can be used to query such information only when
1516 * necessary and update the decoding/displaying accordingly.
1519 int tt = 0, pgcn = 0, pgn = 0;
1521 dvdnav_current_title_program(d->dvdnav, &tt, &pgcn, &pgn);
1524 // Transition to another title signals that we are done.
1530 case DVDNAV_CELL_CHANGE:
1532 * Some status information like the current Title and Part
1533 * numbers do not change inside a cell. Therefore this event
1534 * can be used to query such information only when necessary
1535 * and update the decoding/displaying accordingly.
1538 int tt = 0, pgcn = 0, pgn = 0, c;
1540 dvdnav_current_title_program(d->dvdnav, &tt, &pgcn, &pgn);
1543 // Transition to another title signals that we are done.
1546 c = FindChapterIndex(d->list_chapter, pgcn, pgn);
1547 if (c != d->chapter)
1551 // Some titles end with a 'link' back to the beginning so
1552 // a transition to an earlier chapter means we're done.
1555 chapter = d->chapter = c;
1560 case DVDNAV_NAV_PACKET:
1562 * A NAV packet provides PTS discontinuity information, angle
1563 * linking information and button definitions for DVD menus.
1564 * Angles are handled completely inside libdvdnav. For the
1565 * menus to work, the NAV packet information has to be passed
1566 * to the overlay engine of the player so that it knows the
1567 * dimensions of the button areas.
1570 // mpegdemux expects to get these. I don't think it does
1571 // anything useful with them however.
1573 // The muxers expect to only get chapter 2 and above
1574 // They write chapter 1 when chapter 2 is detected.
1576 b->new_chap = chapter;
1582 case DVDNAV_HOP_CHANNEL:
1584 * This event is issued whenever a non-seamless operation has
1585 * been executed. Applications with fifos should drop the
1586 * fifos content to speed up responsiveness.
1592 * Playback should end here.
1604 /***********************************************************************
1606 ***********************************************************************
1607 * Returns in which chapter the next block to be read is.
1608 * Chapter numbers start at 1.
1609 **********************************************************************/
1610 static int hb_dvdnav_chapter( hb_dvd_t * e )
1612 hb_dvdnav_t * d = &(e->dvdnav);
1613 int32_t t, pgcn, pgn;
1616 if (dvdnav_current_title_program(d->dvdnav, &t, &pgcn, &pgn) != DVDNAV_STATUS_OK)
1620 c = FindChapterIndex( d->list_chapter, pgcn, pgn );
1624 /***********************************************************************
1626 ***********************************************************************
1627 * Closes and frees everything
1628 **********************************************************************/
1629 static void hb_dvdnav_close( hb_dvd_t ** _d )
1631 hb_dvdnav_t * d = &((*_d)->dvdnav);
1633 if( d->dvdnav ) dvdnav_close( d->dvdnav );
1634 if( d->vmg ) ifoClose( d->vmg );
1635 if( d->reader ) DVDClose( d->reader );
1641 /***********************************************************************
1642 * hb_dvdnav_angle_count
1643 ***********************************************************************
1644 * Returns the number of angles supported.
1645 **********************************************************************/
1646 static int hb_dvdnav_angle_count( hb_dvd_t * e )
1648 hb_dvdnav_t * d = &(e->dvdnav);
1649 int current, angle_count;
1651 if (dvdnav_get_angle_info( d->dvdnav, ¤t, &angle_count) != DVDNAV_STATUS_OK)
1653 hb_log("dvdnav_get_angle_info %s", dvdnav_err_to_string(d->dvdnav));
1659 /***********************************************************************
1660 * hb_dvdnav_set_angle
1661 ***********************************************************************
1662 * Sets the angle to read
1663 **********************************************************************/
1664 static void hb_dvdnav_set_angle( hb_dvd_t * e, int angle )
1666 hb_dvdnav_t * d = &(e->dvdnav);
1668 if (dvdnav_angle_change( d->dvdnav, angle) != DVDNAV_STATUS_OK)
1670 hb_log("dvdnav_angle_change %s", dvdnav_err_to_string(d->dvdnav));
1674 /***********************************************************************
1676 ***********************************************************************
1677 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1678 * cell to be read when we will be done with cell_cur.
1679 **********************************************************************/
1680 static int FindChapterIndex( hb_list_t * list, int pgcn, int pgn )
1683 hb_chapter_t *chapter;
1685 count = hb_list_count( list );
1686 for (ii = 0; ii < count; ii++)
1688 chapter = hb_list_item( list, ii );
1689 if (chapter->pgcn == pgcn && chapter->pgn == pgn)
1690 return chapter->index;
1695 /***********************************************************************
1697 ***********************************************************************
1698 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1699 * cell to be read when we will be done with cell_cur.
1700 **********************************************************************/
1701 static int FindNextCell( pgc_t *pgc, int cell_cur )
1706 if( pgc->cell_playback[cell_cur].block_type ==
1707 BLOCK_TYPE_ANGLE_BLOCK )
1710 while( pgc->cell_playback[cell_cur+i].block_mode !=
1711 BLOCK_MODE_LAST_CELL )
1715 cell_next = cell_cur + i + 1;
1716 hb_log( "dvd: Skipping multi-angle cells %d-%d",
1722 cell_next = cell_cur + 1;
1727 /***********************************************************************
1729 ***********************************************************************
1730 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1731 * cell to be read when we will be done with cell_cur.
1732 * Since pg chains can be circularly linked (either from a read error or
1733 * deliberately) pgcn_map tracks program chains we've already seen.
1734 **********************************************************************/
1735 static int NextPgcn( ifo_handle_t *ifo, int pgcn, uint32_t pgcn_map[MAX_PGCN/32] )
1740 pgcn_map[pgcn >> 5] |= (1 << (pgcn & 31));
1742 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
1743 next_pgcn = pgc->next_pgc_nr;
1744 if ( next_pgcn < 1 || next_pgcn >= MAX_PGCN || next_pgcn > ifo->vts_pgcit->nr_of_pgci_srp )
1747 return pgcn_map[next_pgcn >> 5] & (1 << (next_pgcn & 31))? 0 : next_pgcn;
1750 /***********************************************************************
1752 ***********************************************************************
1753 * Pgc links can loop. I track which have been visited in a bit vector
1754 * Initialize the bit vector to empty.
1755 **********************************************************************/
1756 static void PgcWalkInit( uint32_t pgcn_map[MAX_PGCN/32] )
1758 memset(pgcn_map, 0, sizeof(pgcn_map) );
1761 /***********************************************************************
1763 ***********************************************************************
1765 **********************************************************************/
1766 static int dvdtime2msec(dvd_time_t * dt)
1768 double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
1769 double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
1771 ms = (((dt->hour & 0xf0) >> 3) * 5 + (dt->hour & 0x0f)) * 3600000;
1772 ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
1773 ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
1777 ms += ((dt->frame_u & 0x30) >> 3) * 5 +
1778 (dt->frame_u & 0x0f) * 1000.0 / fps;