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/nav_read.h"
15 #define DVD_READ_CACHE 1
17 static char * hb_dvdnav_name( char * path );
18 static hb_dvd_t * hb_dvdnav_init( char * path );
19 static int hb_dvdnav_title_count( hb_dvd_t * d );
20 static hb_title_t * hb_dvdnav_title_scan( hb_dvd_t * d, int t );
21 static int hb_dvdnav_start( hb_dvd_t * d, hb_title_t *title, int chapter );
22 static void hb_dvdnav_stop( hb_dvd_t * d );
23 static int hb_dvdnav_seek( hb_dvd_t * d, float f );
24 static int hb_dvdnav_read( hb_dvd_t * d, hb_buffer_t * b );
25 static int hb_dvdnav_chapter( hb_dvd_t * d );
26 static void hb_dvdnav_close( hb_dvd_t ** _d );
27 static int hb_dvdnav_angle_count( hb_dvd_t * d );
28 static void hb_dvdnav_set_angle( hb_dvd_t * e, int angle );
30 hb_dvd_func_t hb_dvdnav_func =
35 hb_dvdnav_title_count,
42 hb_dvdnav_angle_count,
46 // there can be at most 999 PGCs per title. round that up to the nearest
50 /***********************************************************************
52 **********************************************************************/
53 static void PgcWalkInit( uint32_t pgcn_map[MAX_PGCN/32] );
54 static int FindChapterIndex( hb_list_t * list, int pgcn, int pgn );
55 static int NextPgcn( ifo_handle_t *ifo, int pgcn, uint32_t pgcn_map[MAX_PGCN/32] );
56 static int FindNextCell( pgc_t *pgc, int cell_cur );
57 static int dvdtime2msec( dvd_time_t * );
59 hb_dvd_func_t * hb_dvdnav_methods( void )
61 return &hb_dvdnav_func;
64 static char * hb_dvdnav_name( char * path )
66 static char name[1024];
67 unsigned char unused[1024];
68 dvd_reader_t * reader;
70 reader = DVDOpen( path );
76 if( DVDUDFVolumeInfo( reader, name, sizeof( name ),
77 unused, sizeof( unused ) ) )
87 /***********************************************************************
89 ***********************************************************************
90 * Once dvdnav has entered the 'stopped' state, it can not be revived
91 * dvdnav_reset doesn't work because it doesn't remember the path
92 * So this function re-opens dvdnav
93 **********************************************************************/
94 static int hb_dvdnav_reset( hb_dvdnav_t * d )
97 dvdnav_close( d->dvdnav );
100 if( dvdnav_open(&d->dvdnav, d->path) != DVDNAV_STATUS_OK )
103 * Not an error, may be a stream - which we'll try in a moment.
105 hb_log( "dvd: not a dvd - trying as a stream/file instead" );
109 if (dvdnav_set_readahead_flag(d->dvdnav, DVD_READ_CACHE) !=
112 hb_error("Error: dvdnav_set_readahead_flag: %s\n",
113 dvdnav_err_to_string(d->dvdnav));
118 ** set the PGC positioning flag to have position information
119 ** relatively to the whole feature instead of just relatively to the
122 if (dvdnav_set_PGC_positioning_flag(d->dvdnav, 1) != DVDNAV_STATUS_OK)
124 hb_error("Error: dvdnav_set_PGC_positioning_flag: %s\n",
125 dvdnav_err_to_string(d->dvdnav));
131 if( d->dvdnav ) dvdnav_close( d->dvdnav );
135 /***********************************************************************
137 ***********************************************************************
139 **********************************************************************/
140 static hb_dvd_t * hb_dvdnav_init( char * path )
145 e = calloc( sizeof( hb_dvd_t ), 1 );
149 if( dvdnav_open(&d->dvdnav, path) != DVDNAV_STATUS_OK )
152 * Not an error, may be a stream - which we'll try in a moment.
154 hb_log( "dvd: not a dvd - trying as a stream/file instead" );
158 if (dvdnav_set_readahead_flag(d->dvdnav, DVD_READ_CACHE) !=
161 hb_error("Error: dvdnav_set_readahead_flag: %s\n",
162 dvdnav_err_to_string(d->dvdnav));
167 ** set the PGC positioning flag to have position information
168 ** relatively to the whole feature instead of just relatively to the
171 if (dvdnav_set_PGC_positioning_flag(d->dvdnav, 1) != DVDNAV_STATUS_OK)
173 hb_error("Error: dvdnav_set_PGC_positioning_flag: %s\n",
174 dvdnav_err_to_string(d->dvdnav));
179 if( !( d->reader = DVDOpen( path ) ) )
182 * Not an error, may be a stream - which we'll try in a moment.
184 hb_log( "dvd: not a dvd - trying as a stream/file instead" );
189 if( !( d->vmg = ifoOpen( d->reader, 0 ) ) )
191 hb_error( "dvd: ifoOpen failed" );
195 d->path = strdup( path );
200 if( d->dvdnav ) dvdnav_close( d->dvdnav );
201 if( d->vmg ) ifoClose( d->vmg );
202 if( d->reader ) DVDClose( d->reader );
207 /***********************************************************************
208 * hb_dvdnav_title_count
209 **********************************************************************/
210 static int hb_dvdnav_title_count( hb_dvd_t * e )
213 hb_dvdnav_t * d = &(e->dvdnav);
215 dvdnav_get_number_of_titles(d->dvdnav, &titles);
220 PttDuration(ifo_handle_t *ifo, int ttn, int pttn, int *blocks, int *last_pgcn)
224 uint64_t duration = 0;
225 int cell_start, cell_end;
228 // Initialize map of visited pgc's to prevent loops
229 uint32_t pgcn_map[MAX_PGCN/32];
230 PgcWalkInit( pgcn_map );
231 pgcn = ifo->vts_ptt_srpt->title[ttn-1].ptt[pttn-1].pgcn;
232 pgn = ifo->vts_ptt_srpt->title[ttn-1].ptt[pttn-1].pgn;
233 if ( pgcn < 1 || pgcn > ifo->vts_pgcit->nr_of_pgci_srp || pgcn >= MAX_PGCN)
235 hb_error( "invalid PGC ID %d, skipping", pgcn );
239 if( pgn <= 0 || pgn > 99 )
241 hb_error( "scan: pgn %d not valid, skipping", pgn );
248 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
251 hb_error( "scan: pgc not valid, skipping" );
254 if (pgn > pgc->nr_of_programs)
260 duration += 90LL * dvdtime2msec( &pgc->playback_time );
262 cell_start = pgc->program_map[pgn-1] - 1;
263 cell_end = pgc->nr_of_cells - 1;
264 for(i = cell_start; i <= cell_end; i = FindNextCell(pgc, i))
266 *blocks += pgc->cell_playback[i].last_sector + 1 -
267 pgc->cell_playback[i].first_sector;
271 } while((pgcn = NextPgcn(ifo, pgcn, pgcn_map)) != 0);
275 /***********************************************************************
276 * hb_dvdnav_title_scan
277 **********************************************************************/
278 static hb_title_t * hb_dvdnav_title_scan( hb_dvd_t * e, int t )
281 hb_dvdnav_t * d = &(e->dvdnav);
283 ifo_handle_t * ifo = NULL;
284 int pgcn, pgn, pgcn_end, i, c;
288 hb_chapter_t * chapter;
290 uint64_t duration, longest;
291 int longest_pgcn, longest_pgn, longest_pgcn_end;
292 float duration_correction;
295 hb_log( "scan: scanning title %d", t );
297 title = hb_title_init( d->path, t );
298 if (dvdnav_get_title_string(d->dvdnav, &name) == DVDNAV_STATUS_OK)
300 strncpy( title->name, name, sizeof( title->name ) );
304 char * p_cur, * p_last = d->path;
305 for( p_cur = d->path; *p_cur; p_cur++ )
307 if( p_cur[0] == '/' && p_cur[1] )
312 snprintf( title->name, sizeof( title->name ), "%s", p_last );
315 /* VTS which our title is in */
316 title->vts = d->vmg->tt_srpt->title[t-1].title_set_nr;
320 /* A VTS of 0 means the title wasn't found in the title set */
321 hb_error("Invalid VTS (title set) number: %i", title->vts);
325 hb_log( "scan: opening IFO for VTS %d", title->vts );
326 if( !( ifo = ifoOpen( d->reader, title->vts ) ) )
328 hb_error( "scan: ifoOpen failed" );
332 /* ignore titles with bogus cell addresses so we don't abort later
334 for ( i = 0; i < ifo->vts_c_adt->nr_of_vobs; ++i)
336 if( (ifo->vts_c_adt->cell_adr_table[i].start_sector & 0xffffff ) ==
339 hb_error( "scan: cell_adr_table[%d].start_sector invalid (0x%x) "
340 "- skipping title", i,
341 ifo->vts_c_adt->cell_adr_table[i].start_sector );
344 if( (ifo->vts_c_adt->cell_adr_table[i].last_sector & 0xffffff ) ==
347 hb_error( "scan: cell_adr_table[%d].last_sector invalid (0x%x) "
348 "- skipping title", i,
349 ifo->vts_c_adt->cell_adr_table[i].last_sector );
352 if( ifo->vts_c_adt->cell_adr_table[i].start_sector >=
353 ifo->vts_c_adt->cell_adr_table[i].last_sector )
355 hb_error( "scan: cell_adr_table[%d].start_sector (0x%x) "
356 "is not before last_sector (0x%x) - skipping title", i,
357 ifo->vts_c_adt->cell_adr_table[i].start_sector,
358 ifo->vts_c_adt->cell_adr_table[i].last_sector );
363 if( global_verbosity_level == 3 )
365 ifo_print( d->reader, title->vts );
368 /* Position of the title in the VTS */
369 title->ttn = d->vmg->tt_srpt->title[t-1].vts_ttn;
370 if ( title->ttn < 1 || title->ttn > ifo->vts_ptt_srpt->nr_of_srpts )
372 hb_error( "invalid VTS PTT offset %d for title %d, skipping", title->ttn, t );
379 longest_pgcn_end = -1;
381 for( i = 0; i < ifo->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts; i++ )
385 duration = PttDuration(ifo, title->ttn, i+1, &blocks, &pgcn_end);
386 pgcn = ifo->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgcn;
387 pgn = ifo->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgn;
388 if( duration > longest )
392 longest_pgcn_end = pgcn_end;
394 title->block_count = blocks;
396 else if (pgcn == longest_pgcn && pgn < longest_pgn)
399 title->block_count = blocks;
403 /* ignore titles under 10 seconds because they're often stills or
404 * clips with no audio & our preview code doesn't currently handle
405 * either of these. */
406 if( longest < 900000LL )
408 hb_log( "scan: ignoring title (too short)" );
413 pgcn_end = longest_pgcn_end;
419 if ( pgcn < 1 || pgcn > ifo->vts_pgcit->nr_of_pgci_srp || pgcn >= MAX_PGCN)
421 hb_error( "invalid PGC ID %d for title %d, skipping", pgcn, t );
425 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
427 hb_log("pgc_id: %d, pgn: %d: pgc: 0x%x", pgcn, pgn, pgc);
428 if (pgn > pgc->nr_of_programs)
430 hb_error( "invalid PGN %d for title %d, skipping", pgn, t );
435 title->cell_start = pgc->program_map[pgn-1] - 1;
436 title->block_start = pgc->cell_playback[title->cell_start].first_sector;
438 pgc = ifo->vts_pgcit->pgci_srp[pgcn_end-1].pgc;
441 title->cell_end = pgc->nr_of_cells - 1;
442 title->block_end = pgc->cell_playback[title->cell_end].last_sector;
444 hb_log( "scan: vts=%d, ttn=%d, cells=%d->%d, blocks=%d->%d, "
445 "%d blocks", title->vts, title->ttn, title->cell_start,
446 title->cell_end, title->block_start, title->block_end,
447 title->block_count );
450 title->duration = longest;
451 title->hours = title->duration / 90000 / 3600;
452 title->minutes = ( ( title->duration / 90000 ) % 3600 ) / 60;
453 title->seconds = ( title->duration / 90000 ) % 60;
454 hb_log( "scan: duration is %02d:%02d:%02d (%lld ms)",
455 title->hours, title->minutes, title->seconds,
456 title->duration / 90 );
458 /* Detect languages */
459 for( i = 0; i < ifo->vtsi_mat->nr_of_vts_audio_streams; i++ )
461 hb_audio_t * audio, * audio_tmp;
462 int audio_format, lang_code, audio_control,
464 iso639_lang_t * lang;
465 int lang_extension = 0;
467 hb_log( "scan: checking audio %d", i + 1 );
469 audio = calloc( sizeof( hb_audio_t ), 1 );
471 audio_format = ifo->vtsi_mat->vts_audio_attr[i].audio_format;
472 lang_code = ifo->vtsi_mat->vts_audio_attr[i].lang_code;
473 lang_extension = ifo->vtsi_mat->vts_audio_attr[i].code_extension;
475 ifo->vts_pgcit->pgci_srp[title_pgcn-1].pgc->audio_control[i];
477 if( !( audio_control & 0x8000 ) )
479 hb_log( "scan: audio channel is not active" );
484 position = ( audio_control & 0x7F00 ) >> 8;
486 switch( audio_format )
489 audio->id = ( ( 0x80 + position ) << 8 ) | 0xbd;
490 audio->config.in.codec = HB_ACODEC_AC3;
495 audio->id = 0xc0 + position;
496 audio->config.in.codec = HB_ACODEC_MPGA;
500 audio->id = ( ( 0xa0 + position ) << 8 ) | 0xbd;
501 audio->config.in.codec = HB_ACODEC_LPCM;
505 audio->id = ( ( 0x88 + position ) << 8 ) | 0xbd;
506 audio->config.in.codec = HB_ACODEC_DCA;
511 audio->config.in.codec = 0;
512 hb_log( "scan: unknown audio codec (%x)",
521 /* Check for duplicate tracks */
523 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
525 audio_tmp = hb_list_item( title->list_audio, j );
526 if( audio->id == audio_tmp->id )
534 hb_log( "scan: duplicate audio track" );
539 audio->config.lang.type = lang_extension;
541 lang = lang_for_code( ifo->vtsi_mat->vts_audio_attr[i].lang_code );
543 snprintf( audio->config.lang.description, sizeof( audio->config.lang.description ), "%s (%s)",
544 strlen(lang->native_name) ? lang->native_name : lang->eng_name,
545 audio->config.in.codec == HB_ACODEC_AC3 ? "AC3" : ( audio->config.in.codec ==
546 HB_ACODEC_DCA ? "DTS" : ( audio->config.in.codec ==
547 HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) ) );
548 snprintf( audio->config.lang.simple, sizeof( audio->config.lang.simple ), "%s",
549 strlen(lang->native_name) ? lang->native_name : lang->eng_name );
550 snprintf( audio->config.lang.iso639_2, sizeof( audio->config.lang.iso639_2 ), "%s",
553 switch( lang_extension )
559 strcat( audio->config.lang.description, " (Visually Impaired)" );
562 strcat( audio->config.lang.description, " (Director's Commentary 1)" );
565 strcat( audio->config.lang.description, " (Director's Commentary 2)" );
571 hb_log( "scan: id=%x, lang=%s, 3cc=%s ext=%i", audio->id,
572 audio->config.lang.description, audio->config.lang.iso639_2,
575 audio->config.in.track = i;
576 hb_list_add( title->list_audio, audio );
579 if( !hb_list_count( title->list_audio ) )
581 hb_log( "scan: ignoring title (no audio track)" );
585 memcpy( title->palette,
586 ifo->vts_pgcit->pgci_srp[title_pgcn-1].pgc->palette,
587 16 * sizeof( uint32_t ) );
589 /* Check for subtitles */
590 for( i = 0; i < ifo->vtsi_mat->nr_of_vts_subp_streams; i++ )
592 hb_subtitle_t * subtitle;
595 iso639_lang_t * lang;
596 int lang_extension = 0;
598 hb_log( "scan: checking subtitle %d", i + 1 );
601 ifo->vts_pgcit->pgci_srp[title_pgcn-1].pgc->subp_control[i];
603 if( !( spu_control & 0x80000000 ) )
605 hb_log( "scan: subtitle channel is not active" );
609 if( ifo->vtsi_mat->vts_video_attr.display_aspect_ratio )
611 switch( ifo->vtsi_mat->vts_video_attr.permitted_df )
614 position = spu_control & 0xFF;
617 position = ( spu_control >> 8 ) & 0xFF;
620 position = ( spu_control >> 16 ) & 0xFF;
625 position = ( spu_control >> 24 ) & 0x7F;
628 lang_extension = ifo->vtsi_mat->vts_subp_attr[i].code_extension;
630 lang = lang_for_code( ifo->vtsi_mat->vts_subp_attr[i].lang_code );
632 subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
633 subtitle->track = i+1;
634 subtitle->id = ( ( 0x20 + position ) << 8 ) | 0xbd;
635 snprintf( subtitle->lang, sizeof( subtitle->lang ), "%s",
636 strlen(lang->native_name) ? lang->native_name : lang->eng_name);
637 snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ), "%s",
639 subtitle->format = PICTURESUB;
640 subtitle->source = VOBSUB;
641 subtitle->config.dest = RENDERSUB; // By default render (burn-in) the VOBSUB.
643 subtitle->type = lang_extension;
645 switch( lang_extension )
652 strcat( subtitle->lang, " (Caption with bigger size character)");
655 strcat( subtitle->lang, " (Caption for Children)");
660 strcat( subtitle->lang, " (Closed Caption)");
663 strcat( subtitle->lang, " (Closed Caption with bigger size character)");
666 strcat( subtitle->lang, " (Closed Caption for Children)");
671 strcat( subtitle->lang, " (Forced Caption)");
680 strcat( subtitle->lang, " (Director's Commentary)");
683 strcat( subtitle->lang, " (Director's Commentary with bigger size character)");
686 strcat( subtitle->lang, " (Director's Commentary for Children)");
691 hb_log( "scan: id=%x, lang=%s, 3cc=%s", subtitle->id,
692 subtitle->lang, subtitle->iso639_2 );
694 hb_list_add( title->list_subtitle, subtitle );
698 uint32_t pgcn_map[MAX_PGCN/32];
699 PgcWalkInit( pgcn_map );
703 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
705 for (i = pgn; i <= pgc->nr_of_programs; i++)
707 chapter = calloc( sizeof( hb_chapter_t ), 1 );
709 chapter->index = c + 1;
710 chapter->pgcn = pgcn;
712 hb_list_add( title->list_chapter, chapter );
717 } while ((pgcn = NextPgcn(ifo, pgcn, pgcn_map)) != 0);
719 hb_log( "scan: title %d has %d chapters", t, c );
722 count = hb_list_count( title->list_chapter );
723 for (i = 0; i < count; i++)
725 chapter = hb_list_item( title->list_chapter, i );
727 pgcn = chapter->pgcn;
729 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
732 chapter->cell_start = pgc->program_map[pgn-1] - 1;
733 chapter->block_start = pgc->cell_playback[chapter->cell_start].first_sector;
734 // if there are no more programs in this pgc, the end cell is the
735 // last cell. Otherwise it's the cell before the start cell of the
737 if ( pgn == pgc->nr_of_programs )
739 chapter->cell_end = pgc->nr_of_cells - 1;
743 chapter->cell_end = pgc->program_map[pgn] - 2;;
745 chapter->block_end = pgc->cell_playback[chapter->cell_end].last_sector;
747 /* Block count, duration */
748 chapter->block_count = 0;
749 chapter->duration = 0;
751 cell_cur = chapter->cell_start;
752 while( cell_cur <= chapter->cell_end )
754 #define cp pgc->cell_playback[cell_cur]
755 chapter->block_count += cp.last_sector + 1 - cp.first_sector;
756 chapter->duration += 90LL * dvdtime2msec( &cp.playback_time );
758 cell_cur = FindNextCell( pgc, cell_cur );
760 duration += chapter->duration;
763 /* The durations we get for chapters aren't precise. Scale them so
764 the total matches the title duration */
765 duration_correction = (float) title->duration / (float) duration;
766 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
769 chapter = hb_list_item( title->list_chapter, i );
770 chapter->duration = duration_correction * chapter->duration;
771 seconds = ( chapter->duration + 45000 ) / 90000;
772 chapter->hours = seconds / 3600;
773 chapter->minutes = ( seconds % 3600 ) / 60;
774 chapter->seconds = seconds % 60;
776 hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %lld ms",
777 chapter->index, chapter->cell_start, chapter->cell_end,
778 chapter->block_start, chapter->block_end,
779 chapter->block_count, chapter->duration / 90 );
782 /* Get aspect. We don't get width/height/rate infos here as
783 they tend to be wrong */
784 switch( ifo->vtsi_mat->vts_video_attr.display_aspect_ratio )
787 title->container_aspect = 4. / 3.;
790 title->container_aspect = 16. / 9.;
793 hb_log( "scan: unknown aspect" );
797 hb_log( "scan: aspect = %g", title->aspect );
799 /* This title is ok so far */
803 hb_list_close( &title->list_audio );
808 if( ifo ) ifoClose( ifo );
813 /***********************************************************************
815 ***********************************************************************
816 * Title and chapter start at 1
817 **********************************************************************/
818 static int hb_dvdnav_start( hb_dvd_t * e, hb_title_t *title, int c )
820 hb_dvdnav_t * d = &(e->dvdnav);
821 int t = title->index;
822 hb_chapter_t *chapter;
823 dvdnav_status_t result;
825 d->title_block_count = title->block_count;
826 d->list_chapter = title->list_chapter;
828 if ( d->stopped && !hb_dvdnav_reset(d) )
832 chapter = hb_list_item( title->list_chapter, c - 1);
834 result = dvdnav_program_play(d->dvdnav, t, chapter->pgcn, chapter->pgn);
836 result = dvdnav_part_play(d->dvdnav, t, 1);
837 if (result != DVDNAV_STATUS_OK)
839 hb_error( "dvd: dvdnav_*_play failed - %s",
840 dvdnav_err_to_string(d->dvdnav) );
849 /***********************************************************************
851 ***********************************************************************
853 **********************************************************************/
854 static void hb_dvdnav_stop( hb_dvd_t * e )
858 /***********************************************************************
860 ***********************************************************************
862 **********************************************************************/
863 static int hb_dvdnav_seek( hb_dvd_t * e, float f )
865 hb_dvdnav_t * d = &(e->dvdnav);
866 uint64_t sector = f * d->title_block_count;
867 int result, event, len;
868 uint8_t buf[HB_DVD_READ_BUFFER_SIZE];
876 // XXX the current version of libdvdnav can't seek outside the current
877 // PGC. Check if the place we're seeking to is in a different
878 // PGC. Position there & adjust the offset if so.
879 hb_chapter_t *pgc_change = hb_list_item(d->list_chapter, 0 );
880 for ( ii = 0; ii < hb_list_count( d->list_chapter ); ++ii )
882 hb_chapter_t *chapter = hb_list_item( d->list_chapter, ii );
884 if ( chapter->pgcn != pgc_change->pgcn )
886 // this chapter's in a different pgc from the previous - note the
887 // change so we can make sector offset's be pgc relative.
888 pgc_change = chapter;
890 if ( chapter->block_start <= sector && sector <= chapter->block_end )
892 // this chapter contains the sector we want - see if it's in a
893 // different pgc than the one we're currently in.
894 int32_t title, pgcn, pgn;
895 if (dvdnav_current_title_program( d->dvdnav, &title, &pgcn, &pgn ) != DVDNAV_STATUS_OK)
896 hb_log("dvdnav cur pgcn err: %s", dvdnav_err_to_string(d->dvdnav));
897 if ( d->title != title || chapter->pgcn != pgcn )
899 // this chapter is in a different pgc - switch to it.
900 if (dvdnav_program_play(d->dvdnav, d->title, chapter->pgcn, chapter->pgn) != DVDNAV_STATUS_OK)
901 hb_log("dvdnav prog play err: %s", dvdnav_err_to_string(d->dvdnav));
903 // seek sectors are pgc-relative so remove the pgc start sector.
904 sector -= pgc_change->block_start;
909 // dvdnav will not let you seek or poll current position
910 // till it reaches a certain point in parsing. so we
911 // have to get blocks until we reach a cell
912 // Put an arbitrary limit of 100 blocks on how long we search
913 for (ii = 0; ii < 100 && !done; ii++)
915 result = dvdnav_get_next_block( d->dvdnav, buf, &event, &len );
916 if ( result == DVDNAV_STATUS_ERR )
918 hb_log("dvdnav: Read Error, %s", dvdnav_err_to_string(d->dvdnav));
923 case DVDNAV_BLOCK_OK:
924 case DVDNAV_CELL_CHANGE:
928 case DVDNAV_STILL_FRAME:
929 dvdnav_still_skip( d->dvdnav );
933 dvdnav_wait_skip( d->dvdnav );
937 hb_log("dvdnav: stop encountered during seek");
941 case DVDNAV_HOP_CHANNEL:
942 case DVDNAV_NAV_PACKET:
943 case DVDNAV_VTS_CHANGE:
944 case DVDNAV_HIGHLIGHT:
945 case DVDNAV_AUDIO_STREAM_CHANGE:
946 case DVDNAV_SPU_STREAM_CHANGE:
947 case DVDNAV_SPU_CLUT_CHANGE:
954 if (dvdnav_sector_search(d->dvdnav, sector, SEEK_SET) != DVDNAV_STATUS_OK)
956 hb_error( "dvd: dvdnav_sector_search failed - %s",
957 dvdnav_err_to_string(d->dvdnav) );
963 /***********************************************************************
965 ***********************************************************************
967 **********************************************************************/
968 static int hb_dvdnav_read( hb_dvd_t * e, hb_buffer_t * b )
970 hb_dvdnav_t * d = &(e->dvdnav);
971 int result, event, len;
980 result = dvdnav_get_next_block( d->dvdnav, b->data, &event, &len );
981 if ( result == DVDNAV_STATUS_ERR )
983 hb_log("dvdnav: Read Error, %s", dvdnav_err_to_string(d->dvdnav));
988 case DVDNAV_BLOCK_OK:
989 // We have received a regular block of the currently playing
992 // The muxers expect to only get chapter 2 and above
993 // They write chapter 1 when chapter 2 is detected.
995 b->new_chap = chapter;
1001 * Nothing to do here.
1005 case DVDNAV_STILL_FRAME:
1007 * We have reached a still frame. A real player application
1008 * would wait the amount of time specified by the still's
1009 * length while still handling user input to make menus and
1010 * other interactive stills work. A length of 0xff means an
1011 * indefinite still which has to be skipped indirectly by some
1014 dvdnav_still_skip( d->dvdnav );
1019 * We have reached a point in DVD playback, where timing is
1020 * critical. Player application with internal fifos can
1021 * introduce state inconsistencies, because libdvdnav is
1022 * always the fifo's length ahead in the stream compared to
1023 * what the application sees. Such applications should wait
1024 * until their fifos are empty when they receive this type of
1027 dvdnav_wait_skip( d->dvdnav );
1030 case DVDNAV_SPU_CLUT_CHANGE:
1032 * Player applications should pass the new colour lookup table
1033 * to their SPU decoder
1037 case DVDNAV_SPU_STREAM_CHANGE:
1039 * Player applications should inform their SPU decoder to
1044 case DVDNAV_AUDIO_STREAM_CHANGE:
1046 * Player applications should inform their audio decoder to
1051 case DVDNAV_HIGHLIGHT:
1053 * Player applications should inform their overlay engine to
1054 * highlight the given button
1058 case DVDNAV_VTS_CHANGE:
1060 * Some status information like video aspect and video scale
1061 * permissions do not change inside a VTS. Therefore this
1062 * event can be used to query such information only when
1063 * necessary and update the decoding/displaying accordingly.
1067 case DVDNAV_CELL_CHANGE:
1069 * Some status information like the current Title and Part
1070 * numbers do not change inside a cell. Therefore this event
1071 * can be used to query such information only when necessary
1072 * and update the decoding/displaying accordingly.
1075 int tt = 0, pgcn = 0, pgn = 0, c;
1077 dvdnav_current_title_program(d->dvdnav, &tt, &pgcn, &pgn);
1080 // Transition to another title signals that we are done.
1083 c = FindChapterIndex(d->list_chapter, pgcn, pgn);
1084 if (c != d->chapter)
1088 // Some titles end with a 'link' back to the beginning so
1089 // a transition to an earlier chapter means we're done.
1092 chapter = d->chapter = c;
1097 case DVDNAV_NAV_PACKET:
1099 * A NAV packet provides PTS discontinuity information, angle
1100 * linking information and button definitions for DVD menus.
1101 * Angles are handled completely inside libdvdnav. For the
1102 * menus to work, the NAV packet information has to be passed
1103 * to the overlay engine of the player so that it knows the
1104 * dimensions of the button areas.
1107 // mpegdemux expects to get these. I don't think it does
1108 // anything useful with them however.
1110 // The muxers expect to only get chapter 2 and above
1111 // They write chapter 1 when chapter 2 is detected.
1113 b->new_chap = chapter;
1119 case DVDNAV_HOP_CHANNEL:
1121 * This event is issued whenever a non-seamless operation has
1122 * been executed. Applications with fifos should drop the
1123 * fifos content to speed up responsiveness.
1129 * Playback should end here.
1141 /***********************************************************************
1143 ***********************************************************************
1144 * Returns in which chapter the next block to be read is.
1145 * Chapter numbers start at 1.
1146 **********************************************************************/
1147 static int hb_dvdnav_chapter( hb_dvd_t * e )
1149 hb_dvdnav_t * d = &(e->dvdnav);
1150 int32_t t, pgcn, pgn;
1153 if (dvdnav_current_title_program(d->dvdnav, &t, &pgcn, &pgn) != DVDNAV_STATUS_OK)
1157 c = FindChapterIndex( d->list_chapter, pgcn, pgn );
1161 /***********************************************************************
1163 ***********************************************************************
1164 * Closes and frees everything
1165 **********************************************************************/
1166 static void hb_dvdnav_close( hb_dvd_t ** _d )
1168 hb_dvdnav_t * d = &((*_d)->dvdnav);
1170 if( d->dvdnav ) dvdnav_close( d->dvdnav );
1171 if( d->vmg ) ifoClose( d->vmg );
1172 if( d->reader ) DVDClose( d->reader );
1178 /***********************************************************************
1179 * hb_dvdnav_angle_count
1180 ***********************************************************************
1181 * Returns the number of angles supported.
1182 **********************************************************************/
1183 static int hb_dvdnav_angle_count( hb_dvd_t * e )
1185 hb_dvdnav_t * d = &(e->dvdnav);
1186 int current, angle_count;
1188 if (dvdnav_get_angle_info( d->dvdnav, ¤t, &angle_count) != DVDNAV_STATUS_OK)
1190 hb_log("dvdnav_get_angle_info %s", dvdnav_err_to_string(d->dvdnav));
1196 /***********************************************************************
1197 * hb_dvdnav_set_angle
1198 ***********************************************************************
1199 * Sets the angle to read
1200 **********************************************************************/
1201 static void hb_dvdnav_set_angle( hb_dvd_t * e, int angle )
1203 hb_dvdnav_t * d = &(e->dvdnav);
1205 if (dvdnav_angle_change( d->dvdnav, angle) != DVDNAV_STATUS_OK)
1207 hb_log("dvdnav_angle_change %s", dvdnav_err_to_string(d->dvdnav));
1211 /***********************************************************************
1213 ***********************************************************************
1214 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1215 * cell to be read when we will be done with cell_cur.
1216 **********************************************************************/
1217 static int FindChapterIndex( hb_list_t * list, int pgcn, int pgn )
1220 hb_chapter_t *chapter;
1222 count = hb_list_count( list );
1223 for (ii = 0; ii < count; ii++)
1225 chapter = hb_list_item( list, ii );
1226 if (chapter->pgcn == pgcn && chapter->pgn == pgn)
1227 return chapter->index;
1232 /***********************************************************************
1234 ***********************************************************************
1235 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1236 * cell to be read when we will be done with cell_cur.
1237 **********************************************************************/
1238 static int FindNextCell( pgc_t *pgc, int cell_cur )
1243 if( pgc->cell_playback[cell_cur].block_type ==
1244 BLOCK_TYPE_ANGLE_BLOCK )
1247 while( pgc->cell_playback[cell_cur+i].block_mode !=
1248 BLOCK_MODE_LAST_CELL )
1252 cell_next = cell_cur + i + 1;
1253 hb_log( "dvd: Skipping multi-angle cells %d-%d",
1259 cell_next = cell_cur + 1;
1264 /***********************************************************************
1266 ***********************************************************************
1267 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1268 * cell to be read when we will be done with cell_cur.
1269 * Since pg chains can be circularly linked (either from a read error or
1270 * deliberately) pgcn_map tracks program chains we've already seen.
1271 **********************************************************************/
1272 static int NextPgcn( ifo_handle_t *ifo, int pgcn, uint32_t pgcn_map[MAX_PGCN/32] )
1277 pgcn_map[pgcn >> 5] |= (1 << (pgcn & 31));
1279 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
1280 next_pgcn = pgc->next_pgc_nr;
1281 if ( next_pgcn < 1 || next_pgcn >= MAX_PGCN || next_pgcn > ifo->vts_pgcit->nr_of_pgci_srp )
1284 return pgcn_map[next_pgcn >> 5] & (1 << (next_pgcn & 31))? 0 : next_pgcn;
1287 /***********************************************************************
1289 ***********************************************************************
1290 * Pgc links can loop. I track which have been visited in a bit vector
1291 * Initialize the bit vector to empty.
1292 **********************************************************************/
1293 static void PgcWalkInit( uint32_t pgcn_map[MAX_PGCN/32] )
1295 memset(pgcn_map, 0, sizeof(pgcn_map) );
1298 /***********************************************************************
1300 ***********************************************************************
1302 **********************************************************************/
1303 static int dvdtime2msec(dvd_time_t * dt)
1305 double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
1306 double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
1308 ms = (((dt->hour & 0xf0) >> 3) * 5 + (dt->hour & 0x0f)) * 3600000;
1309 ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
1310 ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
1314 ms += ((dt->frame_u & 0x30) >> 3) * 5 +
1315 (dt->frame_u & 0x0f) * 1000.0 / fps;