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 duration += 90LL * dvdtime2msec( &pgc->playback_time );
256 cell_start = pgc->program_map[pgn-1] - 1;
257 cell_end = pgc->nr_of_cells - 1;
258 for(i = cell_start; i <= cell_end; i = FindNextCell(pgc, i))
260 *blocks += pgc->cell_playback[i].last_sector + 1 -
261 pgc->cell_playback[i].first_sector;
265 } while((pgcn = NextPgcn(ifo, pgcn, pgcn_map)) != 0);
269 /***********************************************************************
270 * hb_dvdnav_title_scan
271 **********************************************************************/
272 static hb_title_t * hb_dvdnav_title_scan( hb_dvd_t * e, int t )
275 hb_dvdnav_t * d = &(e->dvdnav);
277 ifo_handle_t * ifo = NULL;
278 int pgcn, pgn, pgcn_end, i, c;
282 hb_chapter_t * chapter;
284 uint64_t duration, longest;
285 int longest_pgcn, longest_pgn, longest_pgcn_end;
286 float duration_correction;
289 hb_log( "scan: scanning title %d", t );
291 title = hb_title_init( d->path, t );
292 if (dvdnav_get_title_string(d->dvdnav, &name) == DVDNAV_STATUS_OK)
294 strncpy( title->name, name, sizeof( title->name ) );
298 char * p_cur, * p_last = d->path;
299 for( p_cur = d->path; *p_cur; p_cur++ )
301 if( p_cur[0] == '/' && p_cur[1] )
306 snprintf( title->name, sizeof( title->name ), "%s", p_last );
309 /* VTS which our title is in */
310 title->vts = d->vmg->tt_srpt->title[t-1].title_set_nr;
314 /* A VTS of 0 means the title wasn't found in the title set */
315 hb_error("Invalid VTS (title set) number: %i", title->vts);
319 hb_log( "scan: opening IFO for VTS %d", title->vts );
320 if( !( ifo = ifoOpen( d->reader, title->vts ) ) )
322 hb_error( "scan: ifoOpen failed" );
326 /* ignore titles with bogus cell addresses so we don't abort later
328 for ( i = 0; i < ifo->vts_c_adt->nr_of_vobs; ++i)
330 if( (ifo->vts_c_adt->cell_adr_table[i].start_sector & 0xffffff ) ==
333 hb_error( "scan: cell_adr_table[%d].start_sector invalid (0x%x) "
334 "- skipping title", i,
335 ifo->vts_c_adt->cell_adr_table[i].start_sector );
338 if( (ifo->vts_c_adt->cell_adr_table[i].last_sector & 0xffffff ) ==
341 hb_error( "scan: cell_adr_table[%d].last_sector invalid (0x%x) "
342 "- skipping title", i,
343 ifo->vts_c_adt->cell_adr_table[i].last_sector );
346 if( ifo->vts_c_adt->cell_adr_table[i].start_sector >=
347 ifo->vts_c_adt->cell_adr_table[i].last_sector )
349 hb_error( "scan: cell_adr_table[%d].start_sector (0x%x) "
350 "is not before last_sector (0x%x) - skipping title", i,
351 ifo->vts_c_adt->cell_adr_table[i].start_sector,
352 ifo->vts_c_adt->cell_adr_table[i].last_sector );
357 if( global_verbosity_level == 3 )
359 ifo_print( d->reader, title->vts );
362 /* Position of the title in the VTS */
363 title->ttn = d->vmg->tt_srpt->title[t-1].vts_ttn;
364 if ( title->ttn < 1 || title->ttn > ifo->vts_ptt_srpt->nr_of_srpts )
366 hb_error( "invalid VTS PTT offset %d for title %d, skipping", title->ttn, t );
373 longest_pgcn_end = -1;
375 for( i = 0; i < ifo->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts; i++ )
379 duration = PttDuration(ifo, title->ttn, i+1, &blocks, &pgcn_end);
380 pgcn = ifo->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgcn;
381 pgn = ifo->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgn;
382 if( duration > longest )
386 longest_pgcn_end = pgcn_end;
388 title->block_count = blocks;
390 else if (pgcn == longest_pgcn && pgn < longest_pgn)
393 title->block_count = blocks;
397 /* ignore titles under 10 seconds because they're often stills or
398 * clips with no audio & our preview code doesn't currently handle
399 * either of these. */
400 if( longest < 900000LL )
402 hb_log( "scan: ignoring title (too short)" );
407 pgcn_end = longest_pgcn_end;
413 if ( pgcn < 1 || pgcn > ifo->vts_pgcit->nr_of_pgci_srp || pgcn >= MAX_PGCN)
415 hb_error( "invalid PGC ID %d for title %d, skipping", pgcn, t );
419 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
421 hb_log("pgc_id: %d, pgn: %d: pgc: 0x%x", pgcn, pgn, pgc);
424 title->cell_start = pgc->program_map[pgn-1] - 1;
425 title->block_start = pgc->cell_playback[title->cell_start].first_sector;
427 pgc = ifo->vts_pgcit->pgci_srp[pgcn_end-1].pgc;
430 title->cell_end = pgc->nr_of_cells - 1;
431 title->block_end = pgc->cell_playback[title->cell_end].last_sector;
433 hb_log( "scan: vts=%d, ttn=%d, cells=%d->%d, blocks=%d->%d, "
434 "%d blocks", title->vts, title->ttn, title->cell_start,
435 title->cell_end, title->block_start, title->block_end,
436 title->block_count );
439 title->duration = longest;
440 title->hours = title->duration / 90000 / 3600;
441 title->minutes = ( ( title->duration / 90000 ) % 3600 ) / 60;
442 title->seconds = ( title->duration / 90000 ) % 60;
443 hb_log( "scan: duration is %02d:%02d:%02d (%lld ms)",
444 title->hours, title->minutes, title->seconds,
445 title->duration / 90 );
447 /* Detect languages */
448 for( i = 0; i < ifo->vtsi_mat->nr_of_vts_audio_streams; i++ )
450 hb_audio_t * audio, * audio_tmp;
451 int audio_format, lang_code, audio_control,
453 iso639_lang_t * lang;
454 int lang_extension = 0;
456 hb_log( "scan: checking audio %d", i + 1 );
458 audio = calloc( sizeof( hb_audio_t ), 1 );
460 audio_format = ifo->vtsi_mat->vts_audio_attr[i].audio_format;
461 lang_code = ifo->vtsi_mat->vts_audio_attr[i].lang_code;
462 lang_extension = ifo->vtsi_mat->vts_audio_attr[i].code_extension;
464 ifo->vts_pgcit->pgci_srp[title_pgcn-1].pgc->audio_control[i];
466 if( !( audio_control & 0x8000 ) )
468 hb_log( "scan: audio channel is not active" );
473 position = ( audio_control & 0x7F00 ) >> 8;
475 switch( audio_format )
478 audio->id = ( ( 0x80 + position ) << 8 ) | 0xbd;
479 audio->config.in.codec = HB_ACODEC_AC3;
484 audio->id = 0xc0 + position;
485 audio->config.in.codec = HB_ACODEC_MPGA;
489 audio->id = ( ( 0xa0 + position ) << 8 ) | 0xbd;
490 audio->config.in.codec = HB_ACODEC_LPCM;
494 audio->id = ( ( 0x88 + position ) << 8 ) | 0xbd;
495 audio->config.in.codec = HB_ACODEC_DCA;
500 audio->config.in.codec = 0;
501 hb_log( "scan: unknown audio codec (%x)",
510 /* Check for duplicate tracks */
512 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
514 audio_tmp = hb_list_item( title->list_audio, j );
515 if( audio->id == audio_tmp->id )
523 hb_log( "scan: duplicate audio track" );
528 audio->config.lang.type = lang_extension;
530 lang = lang_for_code( ifo->vtsi_mat->vts_audio_attr[i].lang_code );
532 snprintf( audio->config.lang.description, sizeof( audio->config.lang.description ), "%s (%s)",
533 strlen(lang->native_name) ? lang->native_name : lang->eng_name,
534 audio->config.in.codec == HB_ACODEC_AC3 ? "AC3" : ( audio->config.in.codec ==
535 HB_ACODEC_DCA ? "DTS" : ( audio->config.in.codec ==
536 HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) ) );
537 snprintf( audio->config.lang.simple, sizeof( audio->config.lang.simple ), "%s",
538 strlen(lang->native_name) ? lang->native_name : lang->eng_name );
539 snprintf( audio->config.lang.iso639_2, sizeof( audio->config.lang.iso639_2 ), "%s",
542 switch( lang_extension )
548 strcat( audio->config.lang.description, " (Visually Impaired)" );
551 strcat( audio->config.lang.description, " (Director's Commentary 1)" );
554 strcat( audio->config.lang.description, " (Director's Commentary 2)" );
560 hb_log( "scan: id=%x, lang=%s, 3cc=%s ext=%i", audio->id,
561 audio->config.lang.description, audio->config.lang.iso639_2,
564 audio->config.in.track = i;
565 hb_list_add( title->list_audio, audio );
568 if( !hb_list_count( title->list_audio ) )
570 hb_log( "scan: ignoring title (no audio track)" );
574 memcpy( title->palette,
575 ifo->vts_pgcit->pgci_srp[title_pgcn-1].pgc->palette,
576 16 * sizeof( uint32_t ) );
578 /* Check for subtitles */
579 for( i = 0; i < ifo->vtsi_mat->nr_of_vts_subp_streams; i++ )
581 hb_subtitle_t * subtitle;
584 iso639_lang_t * lang;
585 int lang_extension = 0;
587 hb_log( "scan: checking subtitle %d", i + 1 );
590 ifo->vts_pgcit->pgci_srp[title_pgcn-1].pgc->subp_control[i];
592 if( !( spu_control & 0x80000000 ) )
594 hb_log( "scan: subtitle channel is not active" );
598 if( ifo->vtsi_mat->vts_video_attr.display_aspect_ratio )
600 switch( ifo->vtsi_mat->vts_video_attr.permitted_df )
603 position = spu_control & 0xFF;
606 position = ( spu_control >> 8 ) & 0xFF;
609 position = ( spu_control >> 16 ) & 0xFF;
614 position = ( spu_control >> 24 ) & 0x7F;
617 lang_extension = ifo->vtsi_mat->vts_subp_attr[i].code_extension;
619 lang = lang_for_code( ifo->vtsi_mat->vts_subp_attr[i].lang_code );
621 subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
622 subtitle->track = i+1;
623 subtitle->id = ( ( 0x20 + position ) << 8 ) | 0xbd;
624 snprintf( subtitle->lang, sizeof( subtitle->lang ), "%s",
625 strlen(lang->native_name) ? lang->native_name : lang->eng_name);
626 snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ), "%s",
628 subtitle->format = PICTURESUB;
629 subtitle->source = VOBSUB;
630 subtitle->dest = RENDERSUB; // By default render (burn-in) the VOBSUB.
632 subtitle->type = lang_extension;
634 switch( lang_extension )
641 strcat( subtitle->lang, " (Caption with bigger size character)");
644 strcat( subtitle->lang, " (Caption for Children)");
649 strcat( subtitle->lang, " (Closed Caption)");
652 strcat( subtitle->lang, " (Closed Caption with bigger size character)");
655 strcat( subtitle->lang, " (Closed Caption for Children)");
660 strcat( subtitle->lang, " (Forced Caption)");
669 strcat( subtitle->lang, " (Director's Commentary)");
672 strcat( subtitle->lang, " (Director's Commentary with bigger size character)");
675 strcat( subtitle->lang, " (Director's Commentary for Children)");
680 hb_log( "scan: id=%x, lang=%s, 3cc=%s", subtitle->id,
681 subtitle->lang, subtitle->iso639_2 );
683 hb_list_add( title->list_subtitle, subtitle );
687 uint32_t pgcn_map[MAX_PGCN/32];
688 PgcWalkInit( pgcn_map );
692 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
694 for (i = pgn; i <= pgc->nr_of_programs; i++)
696 chapter = calloc( sizeof( hb_chapter_t ), 1 );
698 chapter->index = c + 1;
699 chapter->pgcn = pgcn;
701 hb_list_add( title->list_chapter, chapter );
706 } while ((pgcn = NextPgcn(ifo, pgcn, pgcn_map)) != 0);
708 hb_log( "scan: title %d has %d chapters", t, c );
711 count = hb_list_count( title->list_chapter );
712 for (i = 0; i < count; i++)
714 chapter = hb_list_item( title->list_chapter, i );
716 pgcn = chapter->pgcn;
718 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
721 chapter->cell_start = pgc->program_map[pgn-1] - 1;
722 chapter->block_start = pgc->cell_playback[chapter->cell_start].first_sector;
723 // if there are no more programs in this pgc, the end cell is the
724 // last cell. Otherwise it's the cell before the start cell of the
726 if ( pgn == pgc->nr_of_programs )
728 chapter->cell_end = pgc->nr_of_cells - 1;
732 chapter->cell_end = pgc->program_map[pgn] - 2;;
734 chapter->block_end = pgc->cell_playback[chapter->cell_end].last_sector;
736 /* Block count, duration */
737 chapter->block_count = 0;
738 chapter->duration = 0;
740 cell_cur = chapter->cell_start;
741 while( cell_cur <= chapter->cell_end )
743 #define cp pgc->cell_playback[cell_cur]
744 chapter->block_count += cp.last_sector + 1 - cp.first_sector;
745 chapter->duration += 90LL * dvdtime2msec( &cp.playback_time );
747 cell_cur = FindNextCell( pgc, cell_cur );
749 duration += chapter->duration;
752 /* The durations we get for chapters aren't precise. Scale them so
753 the total matches the title duration */
754 duration_correction = (float) title->duration / (float) duration;
755 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
758 chapter = hb_list_item( title->list_chapter, i );
759 chapter->duration = duration_correction * chapter->duration;
760 seconds = ( chapter->duration + 45000 ) / 90000;
761 chapter->hours = seconds / 3600;
762 chapter->minutes = ( seconds % 3600 ) / 60;
763 chapter->seconds = seconds % 60;
765 hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %lld ms",
766 chapter->index, chapter->cell_start, chapter->cell_end,
767 chapter->block_start, chapter->block_end,
768 chapter->block_count, chapter->duration / 90 );
771 /* Get aspect. We don't get width/height/rate infos here as
772 they tend to be wrong */
773 switch( ifo->vtsi_mat->vts_video_attr.display_aspect_ratio )
776 title->container_aspect = 4. / 3.;
779 title->container_aspect = 16. / 9.;
782 hb_log( "scan: unknown aspect" );
786 hb_log( "scan: aspect = %g", title->aspect );
788 /* This title is ok so far */
792 hb_list_close( &title->list_audio );
797 if( ifo ) ifoClose( ifo );
802 /***********************************************************************
804 ***********************************************************************
805 * Title and chapter start at 1
806 **********************************************************************/
807 static int hb_dvdnav_start( hb_dvd_t * e, hb_title_t *title, int c )
809 hb_dvdnav_t * d = &(e->dvdnav);
810 int t = title->index;
811 hb_chapter_t *chapter;
812 dvdnav_status_t result;
814 d->title_block_count = title->block_count;
815 d->list_chapter = title->list_chapter;
817 if ( d->stopped && !hb_dvdnav_reset(d) )
821 chapter = hb_list_item( title->list_chapter, c - 1);
823 result = dvdnav_program_play(d->dvdnav, t, chapter->pgcn, chapter->pgn);
825 result = dvdnav_part_play(d->dvdnav, t, 1);
826 if (result != DVDNAV_STATUS_OK)
828 hb_error( "dvd: dvdnav_*_play failed - %s",
829 dvdnav_err_to_string(d->dvdnav) );
837 /***********************************************************************
839 ***********************************************************************
841 **********************************************************************/
842 static void hb_dvdnav_stop( hb_dvd_t * e )
846 /***********************************************************************
848 ***********************************************************************
850 **********************************************************************/
851 static int hb_dvdnav_seek( hb_dvd_t * e, float f )
853 hb_dvdnav_t * d = &(e->dvdnav);
854 uint64_t sector = f * d->title_block_count;
855 int result, event, len;
856 uint8_t buf[HB_DVD_READ_BUFFER_SIZE];
864 // XXX the current version of libdvdnav can't seek outside the current
865 // PGC. Check if the place we're seeking to is in a different
866 // PGC. Position there & adjust the offset if so.
867 hb_chapter_t *pgc_change = hb_list_item(d->list_chapter, 0 );
868 for ( ii = 0; ii < hb_list_count( d->list_chapter ); ++ii )
870 hb_chapter_t *chapter = hb_list_item( d->list_chapter, ii );
872 if ( chapter->pgcn != pgc_change->pgcn )
874 // this chapter's in a different pgc from the previous - note the
875 // change so we can make sector offset's be pgc relative.
876 pgc_change = chapter;
878 if ( chapter->block_start <= sector && sector <= chapter->block_end )
880 // this chapter contains the sector we want - see if it's in a
881 // different pgc than the one we're currently in.
882 int32_t title, pgcn, pgn;
883 if (dvdnav_current_title_program( d->dvdnav, &title, &pgcn, &pgn ) != DVDNAV_STATUS_OK)
884 hb_log("dvdnav cur pgcn err: %s", dvdnav_err_to_string(d->dvdnav));
885 if ( d->title != title || chapter->pgcn != pgcn )
887 // this chapter is in a different pgc - switch to it.
888 if (dvdnav_program_play(d->dvdnav, d->title, chapter->pgcn, chapter->pgn) != DVDNAV_STATUS_OK)
889 hb_log("dvdnav prog play err: %s", dvdnav_err_to_string(d->dvdnav));
891 // seek sectors are pgc-relative so remove the pgc start sector.
892 sector -= pgc_change->block_start;
897 // dvdnav will not let you seek or poll current position
898 // till it reaches a certain point in parsing. so we
899 // have to get blocks until we reach a cell
900 // Put an arbitrary limit of 100 blocks on how long we search
901 for (ii = 0; ii < 100 && !done; ii++)
903 result = dvdnav_get_next_block( d->dvdnav, buf, &event, &len );
904 if ( result == DVDNAV_STATUS_ERR )
906 hb_log("dvdnav: Read Error, %s", dvdnav_err_to_string(d->dvdnav));
911 case DVDNAV_BLOCK_OK:
912 case DVDNAV_CELL_CHANGE:
916 case DVDNAV_STILL_FRAME:
917 dvdnav_still_skip( d->dvdnav );
921 dvdnav_wait_skip( d->dvdnav );
925 hb_log("dvdnav: stop encountered during seek");
929 case DVDNAV_HOP_CHANNEL:
930 case DVDNAV_NAV_PACKET:
931 case DVDNAV_VTS_CHANGE:
932 case DVDNAV_HIGHLIGHT:
933 case DVDNAV_AUDIO_STREAM_CHANGE:
934 case DVDNAV_SPU_STREAM_CHANGE:
935 case DVDNAV_SPU_CLUT_CHANGE:
942 if (dvdnav_sector_search(d->dvdnav, sector, SEEK_SET) != DVDNAV_STATUS_OK)
944 hb_error( "dvd: dvdnav_sector_search failed - %s",
945 dvdnav_err_to_string(d->dvdnav) );
951 /***********************************************************************
953 ***********************************************************************
955 **********************************************************************/
956 static int hb_dvdnav_read( hb_dvd_t * e, hb_buffer_t * b )
958 hb_dvdnav_t * d = &(e->dvdnav);
959 int result, event, len;
968 result = dvdnav_get_next_block( d->dvdnav, b->data, &event, &len );
969 if ( result == DVDNAV_STATUS_ERR )
971 hb_log("dvdnav: Read Error, %s", dvdnav_err_to_string(d->dvdnav));
976 case DVDNAV_BLOCK_OK:
977 // We have received a regular block of the currently playing
980 // The muxers expect to only get chapter 2 and above
981 // They write chapter 1 when chapter 2 is detected.
983 b->new_chap = chapter;
989 * Nothing to do here.
993 case DVDNAV_STILL_FRAME:
995 * We have reached a still frame. A real player application
996 * would wait the amount of time specified by the still's
997 * length while still handling user input to make menus and
998 * other interactive stills work. A length of 0xff means an
999 * indefinite still which has to be skipped indirectly by some
1002 dvdnav_still_skip( d->dvdnav );
1007 * We have reached a point in DVD playback, where timing is
1008 * critical. Player application with internal fifos can
1009 * introduce state inconsistencies, because libdvdnav is
1010 * always the fifo's length ahead in the stream compared to
1011 * what the application sees. Such applications should wait
1012 * until their fifos are empty when they receive this type of
1015 dvdnav_wait_skip( d->dvdnav );
1018 case DVDNAV_SPU_CLUT_CHANGE:
1020 * Player applications should pass the new colour lookup table
1021 * to their SPU decoder
1025 case DVDNAV_SPU_STREAM_CHANGE:
1027 * Player applications should inform their SPU decoder to
1032 case DVDNAV_AUDIO_STREAM_CHANGE:
1034 * Player applications should inform their audio decoder to
1039 case DVDNAV_HIGHLIGHT:
1041 * Player applications should inform their overlay engine to
1042 * highlight the given button
1046 case DVDNAV_VTS_CHANGE:
1048 * Some status information like video aspect and video scale
1049 * permissions do not change inside a VTS. Therefore this
1050 * event can be used to query such information only when
1051 * necessary and update the decoding/displaying accordingly.
1055 case DVDNAV_CELL_CHANGE:
1057 * Some status information like the current Title and Part
1058 * numbers do not change inside a cell. Therefore this event
1059 * can be used to query such information only when necessary
1060 * and update the decoding/displaying accordingly.
1063 int tt = 0, pgcn = 0, pgn = 0, c;
1065 dvdnav_current_title_program(d->dvdnav, &tt, &pgcn, &pgn);
1068 // Transition to another title signals that we are done.
1071 c = FindChapterIndex(d->list_chapter, pgcn, pgn);
1073 chapter = d->chapter = c;
1077 case DVDNAV_NAV_PACKET:
1079 * A NAV packet provides PTS discontinuity information, angle
1080 * linking information and button definitions for DVD menus.
1081 * Angles are handled completely inside libdvdnav. For the
1082 * menus to work, the NAV packet information has to be passed
1083 * to the overlay engine of the player so that it knows the
1084 * dimensions of the button areas.
1087 // mpegdemux expects to get these. I don't think it does
1088 // anything useful with them however.
1090 // The muxers expect to only get chapter 2 and above
1091 // They write chapter 1 when chapter 2 is detected.
1093 b->new_chap = chapter;
1099 case DVDNAV_HOP_CHANNEL:
1101 * This event is issued whenever a non-seamless operation has
1102 * been executed. Applications with fifos should drop the
1103 * fifos content to speed up responsiveness.
1109 * Playback should end here.
1121 /***********************************************************************
1123 ***********************************************************************
1124 * Returns in which chapter the next block to be read is.
1125 * Chapter numbers start at 1.
1126 **********************************************************************/
1127 static int hb_dvdnav_chapter( hb_dvd_t * e )
1129 hb_dvdnav_t * d = &(e->dvdnav);
1130 int32_t t, pgcn, pgn;
1133 if (dvdnav_current_title_program(d->dvdnav, &t, &pgcn, &pgn) != DVDNAV_STATUS_OK)
1137 c = FindChapterIndex( d->list_chapter, pgcn, pgn );
1141 /***********************************************************************
1143 ***********************************************************************
1144 * Closes and frees everything
1145 **********************************************************************/
1146 static void hb_dvdnav_close( hb_dvd_t ** _d )
1148 hb_dvdnav_t * d = &((*_d)->dvdnav);
1150 if( d->dvdnav ) dvdnav_close( d->dvdnav );
1151 if( d->vmg ) ifoClose( d->vmg );
1152 if( d->reader ) DVDClose( d->reader );
1158 /***********************************************************************
1159 * hb_dvdnav_angle_count
1160 ***********************************************************************
1161 * Returns the number of angles supported.
1162 **********************************************************************/
1163 static int hb_dvdnav_angle_count( hb_dvd_t * e )
1165 hb_dvdnav_t * d = &(e->dvdnav);
1166 int current, angle_count;
1168 if (dvdnav_get_angle_info( d->dvdnav, ¤t, &angle_count) != DVDNAV_STATUS_OK)
1170 hb_log("dvdnav_get_angle_info %s", dvdnav_err_to_string(d->dvdnav));
1176 /***********************************************************************
1177 * hb_dvdnav_set_angle
1178 ***********************************************************************
1179 * Sets the angle to read
1180 **********************************************************************/
1181 static void hb_dvdnav_set_angle( hb_dvd_t * e, int angle )
1183 hb_dvdnav_t * d = &(e->dvdnav);
1185 if (dvdnav_angle_change( d->dvdnav, angle) != DVDNAV_STATUS_OK)
1187 hb_log("dvdnav_angle_change %s", dvdnav_err_to_string(d->dvdnav));
1191 /***********************************************************************
1193 ***********************************************************************
1194 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1195 * cell to be read when we will be done with cell_cur.
1196 **********************************************************************/
1197 static int FindChapterIndex( hb_list_t * list, int pgcn, int pgn )
1200 hb_chapter_t *chapter;
1202 count = hb_list_count( list );
1203 for (ii = 0; ii < count; ii++)
1205 chapter = hb_list_item( list, ii );
1206 if (chapter->pgcn == pgcn && chapter->pgn == pgn)
1207 return chapter->index;
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 FindNextCell( pgc_t *pgc, int cell_cur )
1223 if( pgc->cell_playback[cell_cur].block_type ==
1224 BLOCK_TYPE_ANGLE_BLOCK )
1227 while( pgc->cell_playback[cell_cur+i].block_mode !=
1228 BLOCK_MODE_LAST_CELL )
1232 cell_next = cell_cur + i + 1;
1233 hb_log( "dvd: Skipping multi-angle cells %d-%d",
1239 cell_next = cell_cur + 1;
1244 /***********************************************************************
1246 ***********************************************************************
1247 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1248 * cell to be read when we will be done with cell_cur.
1249 * Since pg chains can be circularly linked (either from a read error or
1250 * deliberately) pgcn_map tracks program chains we've already seen.
1251 **********************************************************************/
1252 static int NextPgcn( ifo_handle_t *ifo, int pgcn, uint32_t pgcn_map[MAX_PGCN/32] )
1257 pgcn_map[pgcn >> 5] |= (1 << (pgcn & 31));
1259 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
1260 next_pgcn = pgc->next_pgc_nr;
1261 if ( next_pgcn < 1 || next_pgcn >= MAX_PGCN || next_pgcn > ifo->vts_pgcit->nr_of_pgci_srp )
1264 return pgcn_map[next_pgcn >> 5] & (1 << (next_pgcn & 31))? 0 : next_pgcn;
1267 /***********************************************************************
1269 ***********************************************************************
1270 * Pgc links can loop. I track which have been visited in a bit vector
1271 * Initialize the bit vector to empty.
1272 **********************************************************************/
1273 static void PgcWalkInit( uint32_t pgcn_map[MAX_PGCN/32] )
1275 memset(pgcn_map, 0, sizeof(pgcn_map) );
1278 /***********************************************************************
1280 ***********************************************************************
1282 **********************************************************************/
1283 static int dvdtime2msec(dvd_time_t * dt)
1285 double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
1286 double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
1288 ms = (((dt->hour & 0xf0) >> 3) * 5 + (dt->hour & 0x0f)) * 3600000;
1289 ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
1290 ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
1294 ms += ((dt->frame_u & 0x30) >> 3) * 5 +
1295 (dt->frame_u & 0x0f) * 1000.0 / fps;