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 * e, int angle );
31 hb_dvd_func_t hb_dvdnav_func =
36 hb_dvdnav_title_count,
43 hb_dvdnav_angle_count,
47 // there can be at most 999 PGCs per title. round that up to the nearest
51 /***********************************************************************
53 **********************************************************************/
54 static void PgcWalkInit( uint32_t pgcn_map[MAX_PGCN/32] );
55 static int FindChapterIndex( hb_list_t * list, int pgcn, int pgn );
56 static int NextPgcn( ifo_handle_t *ifo, int pgcn, uint32_t pgcn_map[MAX_PGCN/32] );
57 static int FindNextCell( pgc_t *pgc, int cell_cur );
58 static int dvdtime2msec( dvd_time_t * );
60 hb_dvd_func_t * hb_dvdnav_methods( void )
62 return &hb_dvdnav_func;
65 static char * hb_dvdnav_name( char * path )
67 static char name[1024];
68 unsigned char unused[1024];
69 dvd_reader_t * reader;
71 reader = DVDOpen( path );
77 if( DVDUDFVolumeInfo( reader, name, sizeof( name ),
78 unused, sizeof( unused ) ) )
88 /***********************************************************************
90 ***********************************************************************
91 * Once dvdnav has entered the 'stopped' state, it can not be revived
92 * dvdnav_reset doesn't work because it doesn't remember the path
93 * So this function re-opens dvdnav
94 **********************************************************************/
95 static int hb_dvdnav_reset( hb_dvdnav_t * d )
98 dvdnav_close( d->dvdnav );
101 if( dvdnav_open(&d->dvdnav, d->path) != DVDNAV_STATUS_OK )
104 * Not an error, may be a stream - which we'll try in a moment.
106 hb_log( "dvd: not a dvd - trying as a stream/file instead" );
110 if (dvdnav_set_readahead_flag(d->dvdnav, DVD_READ_CACHE) !=
113 hb_error("Error: dvdnav_set_readahead_flag: %s\n",
114 dvdnav_err_to_string(d->dvdnav));
119 ** set the PGC positioning flag to have position information
120 ** relatively to the whole feature instead of just relatively to the
123 if (dvdnav_set_PGC_positioning_flag(d->dvdnav, 1) != DVDNAV_STATUS_OK)
125 hb_error("Error: dvdnav_set_PGC_positioning_flag: %s\n",
126 dvdnav_err_to_string(d->dvdnav));
132 if( d->dvdnav ) dvdnav_close( d->dvdnav );
136 /***********************************************************************
138 ***********************************************************************
140 **********************************************************************/
141 static hb_dvd_t * hb_dvdnav_init( char * path )
146 e = calloc( sizeof( hb_dvd_t ), 1 );
150 if( dvdnav_open(&d->dvdnav, path) != DVDNAV_STATUS_OK )
153 * Not an error, may be a stream - which we'll try in a moment.
155 hb_log( "dvd: not a dvd - trying as a stream/file instead" );
159 if (dvdnav_set_readahead_flag(d->dvdnav, DVD_READ_CACHE) !=
162 hb_error("Error: dvdnav_set_readahead_flag: %s\n",
163 dvdnav_err_to_string(d->dvdnav));
168 ** set the PGC positioning flag to have position information
169 ** relatively to the whole feature instead of just relatively to the
172 if (dvdnav_set_PGC_positioning_flag(d->dvdnav, 1) != DVDNAV_STATUS_OK)
174 hb_error("Error: dvdnav_set_PGC_positioning_flag: %s\n",
175 dvdnav_err_to_string(d->dvdnav));
180 if( !( d->reader = DVDOpen( path ) ) )
183 * Not an error, may be a stream - which we'll try in a moment.
185 hb_log( "dvd: not a dvd - trying as a stream/file instead" );
190 if( !( d->vmg = ifoOpen( d->reader, 0 ) ) )
192 hb_error( "dvd: ifoOpen failed" );
196 d->path = strdup( path );
201 if( d->dvdnav ) dvdnav_close( d->dvdnav );
202 if( d->vmg ) ifoClose( d->vmg );
203 if( d->reader ) DVDClose( d->reader );
208 /***********************************************************************
209 * hb_dvdnav_title_count
210 **********************************************************************/
211 static int hb_dvdnav_title_count( hb_dvd_t * e )
214 hb_dvdnav_t * d = &(e->dvdnav);
216 dvdnav_get_number_of_titles(d->dvdnav, &titles);
221 PttDuration(ifo_handle_t *ifo, int ttn, int pttn, int *blocks, int *last_pgcn)
225 uint64_t duration = 0;
226 int cell_start, cell_end;
229 // Initialize map of visited pgc's to prevent loops
230 uint32_t pgcn_map[MAX_PGCN/32];
231 PgcWalkInit( pgcn_map );
232 pgcn = ifo->vts_ptt_srpt->title[ttn-1].ptt[pttn-1].pgcn;
233 pgn = ifo->vts_ptt_srpt->title[ttn-1].ptt[pttn-1].pgn;
234 if ( pgcn < 1 || pgcn > ifo->vts_pgcit->nr_of_pgci_srp || pgcn >= MAX_PGCN)
236 hb_error( "invalid PGC ID %d, skipping", pgcn );
240 if( pgn <= 0 || pgn > 99 )
242 hb_error( "scan: pgn %d not valid, skipping", pgn );
249 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
252 hb_error( "scan: pgc not valid, skipping" );
255 if (pgn > pgc->nr_of_programs)
261 duration += 90LL * dvdtime2msec( &pgc->playback_time );
263 cell_start = pgc->program_map[pgn-1] - 1;
264 cell_end = pgc->nr_of_cells - 1;
265 for(i = cell_start; i <= cell_end; i = FindNextCell(pgc, i))
267 *blocks += pgc->cell_playback[i].last_sector + 1 -
268 pgc->cell_playback[i].first_sector;
272 } while((pgcn = NextPgcn(ifo, pgcn, pgcn_map)) != 0);
276 /***********************************************************************
277 * hb_dvdnav_title_scan
278 **********************************************************************/
279 static hb_title_t * hb_dvdnav_title_scan( hb_dvd_t * e, int t )
282 hb_dvdnav_t * d = &(e->dvdnav);
284 ifo_handle_t * ifo = NULL;
285 int pgcn, pgn, pgcn_end, i, c;
289 hb_chapter_t * chapter;
291 uint64_t duration, longest;
292 int longest_pgcn, longest_pgn, longest_pgcn_end;
293 float duration_correction;
296 hb_log( "scan: scanning title %d", t );
298 title = hb_title_init( d->path, t );
299 if (dvdnav_get_title_string(d->dvdnav, &name) == DVDNAV_STATUS_OK)
301 strncpy( title->name, name, sizeof( title->name ) );
305 char * p_cur, * p_last = d->path;
306 for( p_cur = d->path; *p_cur; p_cur++ )
308 if( p_cur[0] == '/' && p_cur[1] )
313 snprintf( title->name, sizeof( title->name ), "%s", p_last );
316 /* VTS which our title is in */
317 title->vts = d->vmg->tt_srpt->title[t-1].title_set_nr;
321 /* A VTS of 0 means the title wasn't found in the title set */
322 hb_error("Invalid VTS (title set) number: %i", title->vts);
326 hb_log( "scan: opening IFO for VTS %d", title->vts );
327 if( !( ifo = ifoOpen( d->reader, title->vts ) ) )
329 hb_error( "scan: ifoOpen failed" );
333 /* ignore titles with bogus cell addresses so we don't abort later
335 for ( i = 0; i < ifo->vts_c_adt->nr_of_vobs; ++i)
337 if( (ifo->vts_c_adt->cell_adr_table[i].start_sector & 0xffffff ) ==
340 hb_error( "scan: cell_adr_table[%d].start_sector invalid (0x%x) "
341 "- skipping title", i,
342 ifo->vts_c_adt->cell_adr_table[i].start_sector );
345 if( (ifo->vts_c_adt->cell_adr_table[i].last_sector & 0xffffff ) ==
348 hb_error( "scan: cell_adr_table[%d].last_sector invalid (0x%x) "
349 "- skipping title", i,
350 ifo->vts_c_adt->cell_adr_table[i].last_sector );
353 if( ifo->vts_c_adt->cell_adr_table[i].start_sector >=
354 ifo->vts_c_adt->cell_adr_table[i].last_sector )
356 hb_error( "scan: cell_adr_table[%d].start_sector (0x%x) "
357 "is not before last_sector (0x%x) - skipping title", i,
358 ifo->vts_c_adt->cell_adr_table[i].start_sector,
359 ifo->vts_c_adt->cell_adr_table[i].last_sector );
364 if( global_verbosity_level == 3 )
366 ifo_print( d->reader, title->vts );
369 /* Position of the title in the VTS */
370 title->ttn = d->vmg->tt_srpt->title[t-1].vts_ttn;
371 if ( title->ttn < 1 || title->ttn > ifo->vts_ptt_srpt->nr_of_srpts )
373 hb_error( "invalid VTS PTT offset %d for title %d, skipping", title->ttn, t );
380 longest_pgcn_end = -1;
382 for( i = 0; i < ifo->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts; i++ )
386 duration = PttDuration(ifo, title->ttn, i+1, &blocks, &pgcn_end);
387 pgcn = ifo->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgcn;
388 pgn = ifo->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgn;
389 if( duration > longest )
393 longest_pgcn_end = pgcn_end;
395 title->block_count = blocks;
397 else if (pgcn == longest_pgcn && pgn < longest_pgn)
400 title->block_count = blocks;
404 /* ignore titles under 10 seconds because they're often stills or
405 * clips with no audio & our preview code doesn't currently handle
406 * either of these. */
407 if( longest < 900000LL )
409 hb_log( "scan: ignoring title (too short)" );
414 pgcn_end = longest_pgcn_end;
420 if ( pgcn < 1 || pgcn > ifo->vts_pgcit->nr_of_pgci_srp || pgcn >= MAX_PGCN)
422 hb_error( "invalid PGC ID %d for title %d, skipping", pgcn, t );
426 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
428 hb_log("pgc_id: %d, pgn: %d: pgc: %p", pgcn, pgn, pgc);
429 if (pgn > pgc->nr_of_programs)
431 hb_error( "invalid PGN %d for title %d, skipping", pgn, t );
436 title->cell_start = pgc->program_map[pgn-1] - 1;
437 title->block_start = pgc->cell_playback[title->cell_start].first_sector;
439 pgc = ifo->vts_pgcit->pgci_srp[pgcn_end-1].pgc;
442 title->cell_end = pgc->nr_of_cells - 1;
443 title->block_end = pgc->cell_playback[title->cell_end].last_sector;
445 hb_log( "scan: vts=%d, ttn=%d, cells=%d->%d, blocks=%d->%d, "
446 "%d blocks", title->vts, title->ttn, title->cell_start,
447 title->cell_end, title->block_start, title->block_end,
448 title->block_count );
451 title->duration = longest;
452 title->hours = title->duration / 90000 / 3600;
453 title->minutes = ( ( title->duration / 90000 ) % 3600 ) / 60;
454 title->seconds = ( title->duration / 90000 ) % 60;
455 hb_log( "scan: duration is %02d:%02d:%02d (%"PRId64" ms)",
456 title->hours, title->minutes, title->seconds,
457 title->duration / 90 );
459 /* Detect languages */
460 for( i = 0; i < ifo->vtsi_mat->nr_of_vts_audio_streams; i++ )
462 hb_audio_t * audio, * audio_tmp;
463 int audio_format, lang_code, audio_control,
465 iso639_lang_t * lang;
466 int lang_extension = 0;
468 hb_log( "scan: checking audio %d", i + 1 );
470 audio = calloc( sizeof( hb_audio_t ), 1 );
472 audio_format = ifo->vtsi_mat->vts_audio_attr[i].audio_format;
473 lang_code = ifo->vtsi_mat->vts_audio_attr[i].lang_code;
474 lang_extension = ifo->vtsi_mat->vts_audio_attr[i].code_extension;
476 ifo->vts_pgcit->pgci_srp[title_pgcn-1].pgc->audio_control[i];
478 if( !( audio_control & 0x8000 ) )
480 hb_log( "scan: audio channel is not active" );
485 position = ( audio_control & 0x7F00 ) >> 8;
487 switch( audio_format )
490 audio->id = ( ( 0x80 + position ) << 8 ) | 0xbd;
491 audio->config.in.codec = HB_ACODEC_AC3;
496 audio->id = 0xc0 + position;
497 audio->config.in.codec = HB_ACODEC_MPGA;
501 audio->id = ( ( 0xa0 + position ) << 8 ) | 0xbd;
502 audio->config.in.codec = HB_ACODEC_LPCM;
506 audio->id = ( ( 0x88 + position ) << 8 ) | 0xbd;
507 audio->config.in.codec = HB_ACODEC_DCA;
512 audio->config.in.codec = 0;
513 hb_log( "scan: unknown audio codec (%x)",
522 /* Check for duplicate tracks */
524 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
526 audio_tmp = hb_list_item( title->list_audio, j );
527 if( audio->id == audio_tmp->id )
535 hb_log( "scan: duplicate audio track" );
540 audio->config.lang.type = lang_extension;
542 lang = lang_for_code( ifo->vtsi_mat->vts_audio_attr[i].lang_code );
544 snprintf( audio->config.lang.description, sizeof( audio->config.lang.description ), "%s (%s)",
545 strlen(lang->native_name) ? lang->native_name : lang->eng_name,
546 audio->config.in.codec == HB_ACODEC_AC3 ? "AC3" : ( audio->config.in.codec ==
547 HB_ACODEC_DCA ? "DTS" : ( audio->config.in.codec ==
548 HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) ) );
549 snprintf( audio->config.lang.simple, sizeof( audio->config.lang.simple ), "%s",
550 strlen(lang->native_name) ? lang->native_name : lang->eng_name );
551 snprintf( audio->config.lang.iso639_2, sizeof( audio->config.lang.iso639_2 ), "%s",
554 switch( lang_extension )
560 strcat( audio->config.lang.description, " (Visually Impaired)" );
563 strcat( audio->config.lang.description, " (Director's Commentary 1)" );
566 strcat( audio->config.lang.description, " (Director's Commentary 2)" );
572 hb_log( "scan: id=%x, lang=%s, 3cc=%s ext=%i", audio->id,
573 audio->config.lang.description, audio->config.lang.iso639_2,
576 audio->config.in.track = i;
577 hb_list_add( title->list_audio, audio );
580 if( !hb_list_count( title->list_audio ) )
582 hb_log( "scan: ignoring title (no audio track)" );
586 memcpy( title->palette,
587 ifo->vts_pgcit->pgci_srp[title_pgcn-1].pgc->palette,
588 16 * sizeof( uint32_t ) );
590 /* Check for subtitles */
591 for( i = 0; i < ifo->vtsi_mat->nr_of_vts_subp_streams; i++ )
593 hb_subtitle_t * subtitle;
596 iso639_lang_t * lang;
597 int lang_extension = 0;
599 hb_log( "scan: checking subtitle %d", i + 1 );
602 ifo->vts_pgcit->pgci_srp[title_pgcn-1].pgc->subp_control[i];
604 if( !( spu_control & 0x80000000 ) )
606 hb_log( "scan: subtitle channel is not active" );
610 if( ifo->vtsi_mat->vts_video_attr.display_aspect_ratio )
612 switch( ifo->vtsi_mat->vts_video_attr.permitted_df )
615 position = spu_control & 0xFF;
618 position = ( spu_control >> 8 ) & 0xFF;
621 position = ( spu_control >> 16 ) & 0xFF;
626 position = ( spu_control >> 24 ) & 0x7F;
629 lang_extension = ifo->vtsi_mat->vts_subp_attr[i].code_extension;
631 lang = lang_for_code( ifo->vtsi_mat->vts_subp_attr[i].lang_code );
633 subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
634 subtitle->track = i+1;
635 subtitle->id = ( ( 0x20 + position ) << 8 ) | 0xbd;
636 snprintf( subtitle->lang, sizeof( subtitle->lang ), "%s",
637 strlen(lang->native_name) ? lang->native_name : lang->eng_name);
638 snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ), "%s",
640 subtitle->format = PICTURESUB;
641 subtitle->source = VOBSUB;
642 subtitle->config.dest = RENDERSUB; // By default render (burn-in) the VOBSUB.
644 subtitle->type = lang_extension;
646 switch( lang_extension )
653 strcat( subtitle->lang, " (Caption with bigger size character)");
656 strcat( subtitle->lang, " (Caption for Children)");
661 strcat( subtitle->lang, " (Closed Caption)");
664 strcat( subtitle->lang, " (Closed Caption with bigger size character)");
667 strcat( subtitle->lang, " (Closed Caption for Children)");
672 strcat( subtitle->lang, " (Forced Caption)");
681 strcat( subtitle->lang, " (Director's Commentary)");
684 strcat( subtitle->lang, " (Director's Commentary with bigger size character)");
687 strcat( subtitle->lang, " (Director's Commentary for Children)");
692 hb_log( "scan: id=%x, lang=%s, 3cc=%s", subtitle->id,
693 subtitle->lang, subtitle->iso639_2 );
695 hb_list_add( title->list_subtitle, subtitle );
699 uint32_t pgcn_map[MAX_PGCN/32];
700 PgcWalkInit( pgcn_map );
704 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
706 for (i = pgn; i <= pgc->nr_of_programs; i++)
708 chapter = calloc( sizeof( hb_chapter_t ), 1 );
710 chapter->index = c + 1;
711 chapter->pgcn = pgcn;
713 hb_list_add( title->list_chapter, chapter );
718 } while ((pgcn = NextPgcn(ifo, pgcn, pgcn_map)) != 0);
720 hb_log( "scan: title %d has %d chapters", t, c );
723 count = hb_list_count( title->list_chapter );
724 for (i = 0; i < count; i++)
726 chapter = hb_list_item( title->list_chapter, i );
728 pgcn = chapter->pgcn;
730 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
733 chapter->cell_start = pgc->program_map[pgn-1] - 1;
734 chapter->block_start = pgc->cell_playback[chapter->cell_start].first_sector;
735 // if there are no more programs in this pgc, the end cell is the
736 // last cell. Otherwise it's the cell before the start cell of the
738 if ( pgn == pgc->nr_of_programs )
740 chapter->cell_end = pgc->nr_of_cells - 1;
744 chapter->cell_end = pgc->program_map[pgn] - 2;;
746 chapter->block_end = pgc->cell_playback[chapter->cell_end].last_sector;
748 /* Block count, duration */
749 chapter->block_count = 0;
750 chapter->duration = 0;
752 cell_cur = chapter->cell_start;
753 while( cell_cur <= chapter->cell_end )
755 #define cp pgc->cell_playback[cell_cur]
756 chapter->block_count += cp.last_sector + 1 - cp.first_sector;
757 chapter->duration += 90LL * dvdtime2msec( &cp.playback_time );
759 cell_cur = FindNextCell( pgc, cell_cur );
761 duration += chapter->duration;
764 /* The durations we get for chapters aren't precise. Scale them so
765 the total matches the title duration */
766 duration_correction = (float) title->duration / (float) duration;
767 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
770 chapter = hb_list_item( title->list_chapter, i );
771 chapter->duration = duration_correction * chapter->duration;
772 seconds = ( chapter->duration + 45000 ) / 90000;
773 chapter->hours = seconds / 3600;
774 chapter->minutes = ( seconds % 3600 ) / 60;
775 chapter->seconds = seconds % 60;
777 hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %"PRId64" ms",
778 chapter->index, chapter->cell_start, chapter->cell_end,
779 chapter->block_start, chapter->block_end,
780 chapter->block_count, chapter->duration / 90 );
783 /* Get aspect. We don't get width/height/rate infos here as
784 they tend to be wrong */
785 switch( ifo->vtsi_mat->vts_video_attr.display_aspect_ratio )
788 title->container_aspect = 4. / 3.;
791 title->container_aspect = 16. / 9.;
794 hb_log( "scan: unknown aspect" );
798 hb_log( "scan: aspect = %g", title->aspect );
800 /* This title is ok so far */
804 hb_list_close( &title->list_audio );
809 if( ifo ) ifoClose( ifo );
814 /***********************************************************************
816 ***********************************************************************
817 * Title and chapter start at 1
818 **********************************************************************/
819 static int hb_dvdnav_start( hb_dvd_t * e, hb_title_t *title, int c )
821 hb_dvdnav_t * d = &(e->dvdnav);
822 int t = title->index;
823 hb_chapter_t *chapter;
824 dvdnav_status_t result;
826 d->title_block_count = title->block_count;
827 d->list_chapter = title->list_chapter;
829 if ( d->stopped && !hb_dvdnav_reset(d) )
833 chapter = hb_list_item( title->list_chapter, c - 1);
835 result = dvdnav_program_play(d->dvdnav, t, chapter->pgcn, chapter->pgn);
837 result = dvdnav_part_play(d->dvdnav, t, 1);
838 if (result != DVDNAV_STATUS_OK)
840 hb_error( "dvd: dvdnav_*_play failed - %s",
841 dvdnav_err_to_string(d->dvdnav) );
850 /***********************************************************************
852 ***********************************************************************
854 **********************************************************************/
855 static void hb_dvdnav_stop( hb_dvd_t * e )
859 /***********************************************************************
861 ***********************************************************************
863 **********************************************************************/
864 static int hb_dvdnav_seek( hb_dvd_t * e, float f )
866 hb_dvdnav_t * d = &(e->dvdnav);
867 uint64_t sector = f * d->title_block_count;
868 int result, event, len;
869 uint8_t buf[HB_DVD_READ_BUFFER_SIZE];
877 // XXX the current version of libdvdnav can't seek outside the current
878 // PGC. Check if the place we're seeking to is in a different
879 // PGC. Position there & adjust the offset if so.
880 hb_chapter_t *pgc_change = hb_list_item(d->list_chapter, 0 );
881 for ( ii = 0; ii < hb_list_count( d->list_chapter ); ++ii )
883 hb_chapter_t *chapter = hb_list_item( d->list_chapter, ii );
885 if ( chapter->pgcn != pgc_change->pgcn )
887 // this chapter's in a different pgc from the previous - note the
888 // change so we can make sector offset's be pgc relative.
889 pgc_change = chapter;
891 if ( chapter->block_start <= sector && sector <= chapter->block_end )
893 // this chapter contains the sector we want - see if it's in a
894 // different pgc than the one we're currently in.
895 int32_t title, pgcn, pgn;
896 if (dvdnav_current_title_program( d->dvdnav, &title, &pgcn, &pgn ) != DVDNAV_STATUS_OK)
897 hb_log("dvdnav cur pgcn err: %s", dvdnav_err_to_string(d->dvdnav));
898 if ( d->title != title || chapter->pgcn != pgcn )
900 // this chapter is in a different pgc - switch to it.
901 if (dvdnav_program_play(d->dvdnav, d->title, chapter->pgcn, chapter->pgn) != DVDNAV_STATUS_OK)
902 hb_log("dvdnav prog play err: %s", dvdnav_err_to_string(d->dvdnav));
904 // seek sectors are pgc-relative so remove the pgc start sector.
905 sector -= pgc_change->block_start;
910 // dvdnav will not let you seek or poll current position
911 // till it reaches a certain point in parsing. so we
912 // have to get blocks until we reach a cell
913 // Put an arbitrary limit of 100 blocks on how long we search
914 for (ii = 0; ii < 100 && !done; ii++)
916 result = dvdnav_get_next_block( d->dvdnav, buf, &event, &len );
917 if ( result == DVDNAV_STATUS_ERR )
919 hb_log("dvdnav: Read Error, %s", dvdnav_err_to_string(d->dvdnav));
924 case DVDNAV_BLOCK_OK:
925 case DVDNAV_CELL_CHANGE:
929 case DVDNAV_STILL_FRAME:
930 dvdnav_still_skip( d->dvdnav );
934 dvdnav_wait_skip( d->dvdnav );
938 hb_log("dvdnav: stop encountered during seek");
942 case DVDNAV_HOP_CHANNEL:
943 case DVDNAV_NAV_PACKET:
944 case DVDNAV_VTS_CHANGE:
945 case DVDNAV_HIGHLIGHT:
946 case DVDNAV_AUDIO_STREAM_CHANGE:
947 case DVDNAV_SPU_STREAM_CHANGE:
948 case DVDNAV_SPU_CLUT_CHANGE:
955 if (dvdnav_sector_search(d->dvdnav, sector, SEEK_SET) != DVDNAV_STATUS_OK)
957 hb_error( "dvd: dvdnav_sector_search failed - %s",
958 dvdnav_err_to_string(d->dvdnav) );
964 /***********************************************************************
966 ***********************************************************************
968 **********************************************************************/
969 static int hb_dvdnav_read( hb_dvd_t * e, hb_buffer_t * b )
971 hb_dvdnav_t * d = &(e->dvdnav);
972 int result, event, len;
981 result = dvdnav_get_next_block( d->dvdnav, b->data, &event, &len );
982 if ( result == DVDNAV_STATUS_ERR )
984 hb_log("dvdnav: Read Error, %s", dvdnav_err_to_string(d->dvdnav));
989 case DVDNAV_BLOCK_OK:
990 // We have received a regular block of the currently playing
993 // The muxers expect to only get chapter 2 and above
994 // They write chapter 1 when chapter 2 is detected.
996 b->new_chap = chapter;
1002 * Nothing to do here.
1006 case DVDNAV_STILL_FRAME:
1008 * We have reached a still frame. A real player application
1009 * would wait the amount of time specified by the still's
1010 * length while still handling user input to make menus and
1011 * other interactive stills work. A length of 0xff means an
1012 * indefinite still which has to be skipped indirectly by some
1015 dvdnav_still_skip( d->dvdnav );
1020 * We have reached a point in DVD playback, where timing is
1021 * critical. Player application with internal fifos can
1022 * introduce state inconsistencies, because libdvdnav is
1023 * always the fifo's length ahead in the stream compared to
1024 * what the application sees. Such applications should wait
1025 * until their fifos are empty when they receive this type of
1028 dvdnav_wait_skip( d->dvdnav );
1031 case DVDNAV_SPU_CLUT_CHANGE:
1033 * Player applications should pass the new colour lookup table
1034 * to their SPU decoder
1038 case DVDNAV_SPU_STREAM_CHANGE:
1040 * Player applications should inform their SPU decoder to
1045 case DVDNAV_AUDIO_STREAM_CHANGE:
1047 * Player applications should inform their audio decoder to
1052 case DVDNAV_HIGHLIGHT:
1054 * Player applications should inform their overlay engine to
1055 * highlight the given button
1059 case DVDNAV_VTS_CHANGE:
1061 * Some status information like video aspect and video scale
1062 * permissions do not change inside a VTS. Therefore this
1063 * event can be used to query such information only when
1064 * necessary and update the decoding/displaying accordingly.
1068 case DVDNAV_CELL_CHANGE:
1070 * Some status information like the current Title and Part
1071 * numbers do not change inside a cell. Therefore this event
1072 * can be used to query such information only when necessary
1073 * and update the decoding/displaying accordingly.
1076 int tt = 0, pgcn = 0, pgn = 0, c;
1078 dvdnav_current_title_program(d->dvdnav, &tt, &pgcn, &pgn);
1081 // Transition to another title signals that we are done.
1084 c = FindChapterIndex(d->list_chapter, pgcn, pgn);
1085 if (c != d->chapter)
1089 // Some titles end with a 'link' back to the beginning so
1090 // a transition to an earlier chapter means we're done.
1093 chapter = d->chapter = c;
1098 case DVDNAV_NAV_PACKET:
1100 * A NAV packet provides PTS discontinuity information, angle
1101 * linking information and button definitions for DVD menus.
1102 * Angles are handled completely inside libdvdnav. For the
1103 * menus to work, the NAV packet information has to be passed
1104 * to the overlay engine of the player so that it knows the
1105 * dimensions of the button areas.
1108 // mpegdemux expects to get these. I don't think it does
1109 // anything useful with them however.
1111 // The muxers expect to only get chapter 2 and above
1112 // They write chapter 1 when chapter 2 is detected.
1114 b->new_chap = chapter;
1120 case DVDNAV_HOP_CHANNEL:
1122 * This event is issued whenever a non-seamless operation has
1123 * been executed. Applications with fifos should drop the
1124 * fifos content to speed up responsiveness.
1130 * Playback should end here.
1142 /***********************************************************************
1144 ***********************************************************************
1145 * Returns in which chapter the next block to be read is.
1146 * Chapter numbers start at 1.
1147 **********************************************************************/
1148 static int hb_dvdnav_chapter( hb_dvd_t * e )
1150 hb_dvdnav_t * d = &(e->dvdnav);
1151 int32_t t, pgcn, pgn;
1154 if (dvdnav_current_title_program(d->dvdnav, &t, &pgcn, &pgn) != DVDNAV_STATUS_OK)
1158 c = FindChapterIndex( d->list_chapter, pgcn, pgn );
1162 /***********************************************************************
1164 ***********************************************************************
1165 * Closes and frees everything
1166 **********************************************************************/
1167 static void hb_dvdnav_close( hb_dvd_t ** _d )
1169 hb_dvdnav_t * d = &((*_d)->dvdnav);
1171 if( d->dvdnav ) dvdnav_close( d->dvdnav );
1172 if( d->vmg ) ifoClose( d->vmg );
1173 if( d->reader ) DVDClose( d->reader );
1179 /***********************************************************************
1180 * hb_dvdnav_angle_count
1181 ***********************************************************************
1182 * Returns the number of angles supported.
1183 **********************************************************************/
1184 static int hb_dvdnav_angle_count( hb_dvd_t * e )
1186 hb_dvdnav_t * d = &(e->dvdnav);
1187 int current, angle_count;
1189 if (dvdnav_get_angle_info( d->dvdnav, ¤t, &angle_count) != DVDNAV_STATUS_OK)
1191 hb_log("dvdnav_get_angle_info %s", dvdnav_err_to_string(d->dvdnav));
1197 /***********************************************************************
1198 * hb_dvdnav_set_angle
1199 ***********************************************************************
1200 * Sets the angle to read
1201 **********************************************************************/
1202 static void hb_dvdnav_set_angle( hb_dvd_t * e, int angle )
1204 hb_dvdnav_t * d = &(e->dvdnav);
1206 if (dvdnav_angle_change( d->dvdnav, angle) != DVDNAV_STATUS_OK)
1208 hb_log("dvdnav_angle_change %s", dvdnav_err_to_string(d->dvdnav));
1212 /***********************************************************************
1214 ***********************************************************************
1215 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1216 * cell to be read when we will be done with cell_cur.
1217 **********************************************************************/
1218 static int FindChapterIndex( hb_list_t * list, int pgcn, int pgn )
1221 hb_chapter_t *chapter;
1223 count = hb_list_count( list );
1224 for (ii = 0; ii < count; ii++)
1226 chapter = hb_list_item( list, ii );
1227 if (chapter->pgcn == pgcn && chapter->pgn == pgn)
1228 return chapter->index;
1233 /***********************************************************************
1235 ***********************************************************************
1236 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1237 * cell to be read when we will be done with cell_cur.
1238 **********************************************************************/
1239 static int FindNextCell( pgc_t *pgc, int cell_cur )
1244 if( pgc->cell_playback[cell_cur].block_type ==
1245 BLOCK_TYPE_ANGLE_BLOCK )
1248 while( pgc->cell_playback[cell_cur+i].block_mode !=
1249 BLOCK_MODE_LAST_CELL )
1253 cell_next = cell_cur + i + 1;
1254 hb_log( "dvd: Skipping multi-angle cells %d-%d",
1260 cell_next = cell_cur + 1;
1265 /***********************************************************************
1267 ***********************************************************************
1268 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1269 * cell to be read when we will be done with cell_cur.
1270 * Since pg chains can be circularly linked (either from a read error or
1271 * deliberately) pgcn_map tracks program chains we've already seen.
1272 **********************************************************************/
1273 static int NextPgcn( ifo_handle_t *ifo, int pgcn, uint32_t pgcn_map[MAX_PGCN/32] )
1278 pgcn_map[pgcn >> 5] |= (1 << (pgcn & 31));
1280 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
1281 next_pgcn = pgc->next_pgc_nr;
1282 if ( next_pgcn < 1 || next_pgcn >= MAX_PGCN || next_pgcn > ifo->vts_pgcit->nr_of_pgci_srp )
1285 return pgcn_map[next_pgcn >> 5] & (1 << (next_pgcn & 31))? 0 : next_pgcn;
1288 /***********************************************************************
1290 ***********************************************************************
1291 * Pgc links can loop. I track which have been visited in a bit vector
1292 * Initialize the bit vector to empty.
1293 **********************************************************************/
1294 static void PgcWalkInit( uint32_t pgcn_map[MAX_PGCN/32] )
1296 memset(pgcn_map, 0, sizeof(pgcn_map) );
1299 /***********************************************************************
1301 ***********************************************************************
1303 **********************************************************************/
1304 static int dvdtime2msec(dvd_time_t * dt)
1306 double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
1307 double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
1309 ms = (((dt->hour & 0xf0) >> 3) * 5 + (dt->hour & 0x0f)) * 3600000;
1310 ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
1311 ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
1315 ms += ((dt->frame_u & 0x30) >> 3) * 5 +
1316 (dt->frame_u & 0x0f) * 1000.0 / fps;