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 )
147 e = calloc( sizeof( hb_dvd_t ), 1 );
150 /* Log DVD drive region code */
151 if ( hb_dvd_region( path, ®ion_mask ) == 0 )
153 hb_log( "dvd: Region mask 0x%02x", region_mask );
154 if ( region_mask == 0xFF )
156 hb_log( "dvd: Warning, DVD device has no region set" );
161 if( dvdnav_open(&d->dvdnav, path) != DVDNAV_STATUS_OK )
164 * Not an error, may be a stream - which we'll try in a moment.
166 hb_log( "dvd: not a dvd - trying as a stream/file instead" );
170 if (dvdnav_set_readahead_flag(d->dvdnav, DVD_READ_CACHE) !=
173 hb_error("Error: dvdnav_set_readahead_flag: %s\n",
174 dvdnav_err_to_string(d->dvdnav));
179 ** set the PGC positioning flag to have position information
180 ** relatively to the whole feature instead of just relatively to the
183 if (dvdnav_set_PGC_positioning_flag(d->dvdnav, 1) != DVDNAV_STATUS_OK)
185 hb_error("Error: dvdnav_set_PGC_positioning_flag: %s\n",
186 dvdnav_err_to_string(d->dvdnav));
191 if( !( d->reader = DVDOpen( path ) ) )
194 * Not an error, may be a stream - which we'll try in a moment.
196 hb_log( "dvd: not a dvd - trying as a stream/file instead" );
201 if( !( d->vmg = ifoOpen( d->reader, 0 ) ) )
203 hb_error( "dvd: ifoOpen failed" );
207 d->path = strdup( path );
212 if( d->dvdnav ) dvdnav_close( d->dvdnav );
213 if( d->vmg ) ifoClose( d->vmg );
214 if( d->reader ) DVDClose( d->reader );
219 /***********************************************************************
220 * hb_dvdnav_title_count
221 **********************************************************************/
222 static int hb_dvdnav_title_count( hb_dvd_t * e )
225 hb_dvdnav_t * d = &(e->dvdnav);
227 dvdnav_get_number_of_titles(d->dvdnav, &titles);
232 PttDuration(ifo_handle_t *ifo, int ttn, int pttn, int *blocks, int *last_pgcn)
236 uint64_t duration = 0;
237 int cell_start, cell_end;
240 // Initialize map of visited pgc's to prevent loops
241 uint32_t pgcn_map[MAX_PGCN/32];
242 PgcWalkInit( pgcn_map );
243 pgcn = ifo->vts_ptt_srpt->title[ttn-1].ptt[pttn-1].pgcn;
244 pgn = ifo->vts_ptt_srpt->title[ttn-1].ptt[pttn-1].pgn;
245 if ( pgcn < 1 || pgcn > ifo->vts_pgcit->nr_of_pgci_srp || pgcn >= MAX_PGCN)
247 hb_error( "invalid PGC ID %d, skipping", pgcn );
251 if( pgn <= 0 || pgn > 99 )
253 hb_error( "scan: pgn %d not valid, skipping", pgn );
260 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
263 hb_error( "scan: pgc not valid, skipping" );
266 if (pgn > pgc->nr_of_programs)
272 duration += 90LL * dvdtime2msec( &pgc->playback_time );
274 cell_start = pgc->program_map[pgn-1] - 1;
275 cell_end = pgc->nr_of_cells - 1;
276 for(i = cell_start; i <= cell_end; i = FindNextCell(pgc, i))
278 *blocks += pgc->cell_playback[i].last_sector + 1 -
279 pgc->cell_playback[i].first_sector;
283 } while((pgcn = NextPgcn(ifo, pgcn, pgcn_map)) != 0);
287 /***********************************************************************
288 * hb_dvdnav_title_scan
289 **********************************************************************/
290 static hb_title_t * hb_dvdnav_title_scan( hb_dvd_t * e, int t )
293 hb_dvdnav_t * d = &(e->dvdnav);
295 ifo_handle_t * ifo = NULL;
296 int pgcn, pgn, pgcn_end, i, c;
300 hb_chapter_t * chapter;
302 uint64_t duration, longest;
303 int longest_pgcn, longest_pgn, longest_pgcn_end;
304 float duration_correction;
307 hb_log( "scan: scanning title %d", t );
309 title = hb_title_init( d->path, t );
310 if (dvdnav_get_title_string(d->dvdnav, &name) == DVDNAV_STATUS_OK)
312 strncpy( title->name, name, sizeof( title->name ) );
316 char * p_cur, * p_last = d->path;
317 for( p_cur = d->path; *p_cur; p_cur++ )
319 if( p_cur[0] == '/' && p_cur[1] )
324 snprintf( title->name, sizeof( title->name ), "%s", p_last );
327 /* VTS which our title is in */
328 title->vts = d->vmg->tt_srpt->title[t-1].title_set_nr;
332 /* A VTS of 0 means the title wasn't found in the title set */
333 hb_error("Invalid VTS (title set) number: %i", title->vts);
337 hb_log( "scan: opening IFO for VTS %d", title->vts );
338 if( !( ifo = ifoOpen( d->reader, title->vts ) ) )
340 hb_error( "scan: ifoOpen failed" );
344 /* ignore titles with bogus cell addresses so we don't abort later
346 for ( i = 0; i < ifo->vts_c_adt->nr_of_vobs; ++i)
348 if( (ifo->vts_c_adt->cell_adr_table[i].start_sector & 0xffffff ) ==
351 hb_error( "scan: cell_adr_table[%d].start_sector invalid (0x%x) "
352 "- skipping title", i,
353 ifo->vts_c_adt->cell_adr_table[i].start_sector );
356 if( (ifo->vts_c_adt->cell_adr_table[i].last_sector & 0xffffff ) ==
359 hb_error( "scan: cell_adr_table[%d].last_sector invalid (0x%x) "
360 "- skipping title", i,
361 ifo->vts_c_adt->cell_adr_table[i].last_sector );
364 if( ifo->vts_c_adt->cell_adr_table[i].start_sector >=
365 ifo->vts_c_adt->cell_adr_table[i].last_sector )
367 hb_error( "scan: cell_adr_table[%d].start_sector (0x%x) "
368 "is not before last_sector (0x%x) - skipping title", i,
369 ifo->vts_c_adt->cell_adr_table[i].start_sector,
370 ifo->vts_c_adt->cell_adr_table[i].last_sector );
375 if( global_verbosity_level == 3 )
377 ifo_print( d->reader, title->vts );
380 /* Position of the title in the VTS */
381 title->ttn = d->vmg->tt_srpt->title[t-1].vts_ttn;
382 if ( title->ttn < 1 || title->ttn > ifo->vts_ptt_srpt->nr_of_srpts )
384 hb_error( "invalid VTS PTT offset %d for title %d, skipping", title->ttn, t );
391 longest_pgcn_end = -1;
393 for( i = 0; i < ifo->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts; i++ )
397 duration = PttDuration(ifo, title->ttn, i+1, &blocks, &pgcn_end);
398 pgcn = ifo->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgcn;
399 pgn = ifo->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgn;
400 if( duration > longest )
404 longest_pgcn_end = pgcn_end;
406 title->block_count = blocks;
408 else if (pgcn == longest_pgcn && pgn < longest_pgn)
411 title->block_count = blocks;
415 /* ignore titles under 10 seconds because they're often stills or
416 * clips with no audio & our preview code doesn't currently handle
417 * either of these. */
418 if( longest < 900000LL )
420 hb_log( "scan: ignoring title (too short)" );
425 pgcn_end = longest_pgcn_end;
431 if ( pgcn < 1 || pgcn > ifo->vts_pgcit->nr_of_pgci_srp || pgcn >= MAX_PGCN)
433 hb_error( "invalid PGC ID %d for title %d, skipping", pgcn, t );
437 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
439 hb_log("pgc_id: %d, pgn: %d: pgc: %p", pgcn, pgn, pgc);
440 if (pgn > pgc->nr_of_programs)
442 hb_error( "invalid PGN %d for title %d, skipping", pgn, t );
447 title->cell_start = pgc->program_map[pgn-1] - 1;
448 title->block_start = pgc->cell_playback[title->cell_start].first_sector;
450 pgc = ifo->vts_pgcit->pgci_srp[pgcn_end-1].pgc;
453 title->cell_end = pgc->nr_of_cells - 1;
454 title->block_end = pgc->cell_playback[title->cell_end].last_sector;
456 hb_log( "scan: vts=%d, ttn=%d, cells=%d->%d, blocks=%d->%d, "
457 "%d blocks", title->vts, title->ttn, title->cell_start,
458 title->cell_end, title->block_start, title->block_end,
459 title->block_count );
462 title->duration = longest;
463 title->hours = title->duration / 90000 / 3600;
464 title->minutes = ( ( title->duration / 90000 ) % 3600 ) / 60;
465 title->seconds = ( title->duration / 90000 ) % 60;
466 hb_log( "scan: duration is %02d:%02d:%02d (%"PRId64" ms)",
467 title->hours, title->minutes, title->seconds,
468 title->duration / 90 );
470 /* Detect languages */
471 for( i = 0; i < ifo->vtsi_mat->nr_of_vts_audio_streams; i++ )
473 hb_audio_t * audio, * audio_tmp;
474 int audio_format, lang_code, audio_control,
476 iso639_lang_t * lang;
477 int lang_extension = 0;
479 hb_log( "scan: checking audio %d", i + 1 );
481 audio = calloc( sizeof( hb_audio_t ), 1 );
483 audio_format = ifo->vtsi_mat->vts_audio_attr[i].audio_format;
484 lang_code = ifo->vtsi_mat->vts_audio_attr[i].lang_code;
485 lang_extension = ifo->vtsi_mat->vts_audio_attr[i].code_extension;
487 ifo->vts_pgcit->pgci_srp[title_pgcn-1].pgc->audio_control[i];
489 if( !( audio_control & 0x8000 ) )
491 hb_log( "scan: audio channel is not active" );
496 position = ( audio_control & 0x7F00 ) >> 8;
498 switch( audio_format )
501 audio->id = ( ( 0x80 + position ) << 8 ) | 0xbd;
502 audio->config.in.codec = HB_ACODEC_AC3;
507 audio->id = 0xc0 + position;
508 audio->config.in.codec = HB_ACODEC_MPGA;
512 audio->id = ( ( 0xa0 + position ) << 8 ) | 0xbd;
513 audio->config.in.codec = HB_ACODEC_LPCM;
517 audio->id = ( ( 0x88 + position ) << 8 ) | 0xbd;
518 audio->config.in.codec = HB_ACODEC_DCA;
523 audio->config.in.codec = 0;
524 hb_log( "scan: unknown audio codec (%x)",
533 /* Check for duplicate tracks */
535 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
537 audio_tmp = hb_list_item( title->list_audio, j );
538 if( audio->id == audio_tmp->id )
546 hb_log( "scan: duplicate audio track" );
551 audio->config.lang.type = lang_extension;
553 lang = lang_for_code( ifo->vtsi_mat->vts_audio_attr[i].lang_code );
555 snprintf( audio->config.lang.description, sizeof( audio->config.lang.description ), "%s (%s)",
556 strlen(lang->native_name) ? lang->native_name : lang->eng_name,
557 audio->config.in.codec == HB_ACODEC_AC3 ? "AC3" : ( audio->config.in.codec ==
558 HB_ACODEC_DCA ? "DTS" : ( audio->config.in.codec ==
559 HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) ) );
560 snprintf( audio->config.lang.simple, sizeof( audio->config.lang.simple ), "%s",
561 strlen(lang->native_name) ? lang->native_name : lang->eng_name );
562 snprintf( audio->config.lang.iso639_2, sizeof( audio->config.lang.iso639_2 ), "%s",
565 switch( lang_extension )
571 strcat( audio->config.lang.description, " (Visually Impaired)" );
574 strcat( audio->config.lang.description, " (Director's Commentary 1)" );
577 strcat( audio->config.lang.description, " (Director's Commentary 2)" );
583 hb_log( "scan: id=%x, lang=%s, 3cc=%s ext=%i", audio->id,
584 audio->config.lang.description, audio->config.lang.iso639_2,
587 audio->config.in.track = i;
588 hb_list_add( title->list_audio, audio );
591 if( !hb_list_count( title->list_audio ) )
593 hb_log( "scan: ignoring title (no audio track)" );
597 memcpy( title->palette,
598 ifo->vts_pgcit->pgci_srp[title_pgcn-1].pgc->palette,
599 16 * sizeof( uint32_t ) );
601 /* Check for subtitles */
602 for( i = 0; i < ifo->vtsi_mat->nr_of_vts_subp_streams; i++ )
604 hb_subtitle_t * subtitle;
607 iso639_lang_t * lang;
608 int lang_extension = 0;
610 hb_log( "scan: checking subtitle %d", i + 1 );
613 ifo->vts_pgcit->pgci_srp[title_pgcn-1].pgc->subp_control[i];
615 if( !( spu_control & 0x80000000 ) )
617 hb_log( "scan: subtitle channel is not active" );
621 if( ifo->vtsi_mat->vts_video_attr.display_aspect_ratio )
623 switch( ifo->vtsi_mat->vts_video_attr.permitted_df )
626 position = spu_control & 0xFF;
629 position = ( spu_control >> 8 ) & 0xFF;
632 position = ( spu_control >> 16 ) & 0xFF;
637 position = ( spu_control >> 24 ) & 0x7F;
640 lang_extension = ifo->vtsi_mat->vts_subp_attr[i].code_extension;
642 lang = lang_for_code( ifo->vtsi_mat->vts_subp_attr[i].lang_code );
644 subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
645 subtitle->track = i+1;
646 subtitle->id = ( ( 0x20 + position ) << 8 ) | 0xbd;
647 snprintf( subtitle->lang, sizeof( subtitle->lang ), "%s",
648 strlen(lang->native_name) ? lang->native_name : lang->eng_name);
649 snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ), "%s",
651 subtitle->format = PICTURESUB;
652 subtitle->source = VOBSUB;
653 subtitle->config.dest = RENDERSUB; // By default render (burn-in) the VOBSUB.
655 subtitle->type = lang_extension;
657 switch( lang_extension )
664 strcat( subtitle->lang, " (Caption with bigger size character)");
667 strcat( subtitle->lang, " (Caption for Children)");
672 strcat( subtitle->lang, " (Closed Caption)");
675 strcat( subtitle->lang, " (Closed Caption with bigger size character)");
678 strcat( subtitle->lang, " (Closed Caption for Children)");
683 strcat( subtitle->lang, " (Forced Caption)");
692 strcat( subtitle->lang, " (Director's Commentary)");
695 strcat( subtitle->lang, " (Director's Commentary with bigger size character)");
698 strcat( subtitle->lang, " (Director's Commentary for Children)");
703 hb_log( "scan: id=%x, lang=%s, 3cc=%s", subtitle->id,
704 subtitle->lang, subtitle->iso639_2 );
706 hb_list_add( title->list_subtitle, subtitle );
710 uint32_t pgcn_map[MAX_PGCN/32];
711 PgcWalkInit( pgcn_map );
715 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
717 for (i = pgn; i <= pgc->nr_of_programs; i++)
719 chapter = calloc( sizeof( hb_chapter_t ), 1 );
721 chapter->index = c + 1;
722 chapter->pgcn = pgcn;
724 hb_list_add( title->list_chapter, chapter );
729 } while ((pgcn = NextPgcn(ifo, pgcn, pgcn_map)) != 0);
731 hb_log( "scan: title %d has %d chapters", t, c );
734 count = hb_list_count( title->list_chapter );
735 for (i = 0; i < count; i++)
737 chapter = hb_list_item( title->list_chapter, i );
739 pgcn = chapter->pgcn;
741 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
744 chapter->cell_start = pgc->program_map[pgn-1] - 1;
745 chapter->block_start = pgc->cell_playback[chapter->cell_start].first_sector;
746 // if there are no more programs in this pgc, the end cell is the
747 // last cell. Otherwise it's the cell before the start cell of the
749 if ( pgn == pgc->nr_of_programs )
751 chapter->cell_end = pgc->nr_of_cells - 1;
755 chapter->cell_end = pgc->program_map[pgn] - 2;;
757 chapter->block_end = pgc->cell_playback[chapter->cell_end].last_sector;
759 /* Block count, duration */
760 chapter->block_count = 0;
761 chapter->duration = 0;
763 cell_cur = chapter->cell_start;
764 while( cell_cur <= chapter->cell_end )
766 #define cp pgc->cell_playback[cell_cur]
767 chapter->block_count += cp.last_sector + 1 - cp.first_sector;
768 chapter->duration += 90LL * dvdtime2msec( &cp.playback_time );
770 cell_cur = FindNextCell( pgc, cell_cur );
772 duration += chapter->duration;
775 /* The durations we get for chapters aren't precise. Scale them so
776 the total matches the title duration */
777 duration_correction = (float) title->duration / (float) duration;
778 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
781 chapter = hb_list_item( title->list_chapter, i );
782 chapter->duration = duration_correction * chapter->duration;
783 seconds = ( chapter->duration + 45000 ) / 90000;
784 chapter->hours = seconds / 3600;
785 chapter->minutes = ( seconds % 3600 ) / 60;
786 chapter->seconds = seconds % 60;
788 hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %"PRId64" ms",
789 chapter->index, chapter->cell_start, chapter->cell_end,
790 chapter->block_start, chapter->block_end,
791 chapter->block_count, chapter->duration / 90 );
794 /* Get aspect. We don't get width/height/rate infos here as
795 they tend to be wrong */
796 switch( ifo->vtsi_mat->vts_video_attr.display_aspect_ratio )
799 title->container_aspect = 4. / 3.;
802 title->container_aspect = 16. / 9.;
805 hb_log( "scan: unknown aspect" );
809 hb_log( "scan: aspect = %g", title->aspect );
811 /* This title is ok so far */
815 hb_list_close( &title->list_audio );
820 if( ifo ) ifoClose( ifo );
825 /***********************************************************************
827 ***********************************************************************
828 * Title and chapter start at 1
829 **********************************************************************/
830 static int hb_dvdnav_start( hb_dvd_t * e, hb_title_t *title, int c )
832 hb_dvdnav_t * d = &(e->dvdnav);
833 int t = title->index;
834 hb_chapter_t *chapter;
835 dvdnav_status_t result;
837 d->title_block_count = title->block_count;
838 d->list_chapter = title->list_chapter;
840 if ( d->stopped && !hb_dvdnav_reset(d) )
844 chapter = hb_list_item( title->list_chapter, c - 1);
846 result = dvdnav_program_play(d->dvdnav, t, chapter->pgcn, chapter->pgn);
848 result = dvdnav_part_play(d->dvdnav, t, 1);
849 if (result != DVDNAV_STATUS_OK)
851 hb_error( "dvd: dvdnav_*_play failed - %s",
852 dvdnav_err_to_string(d->dvdnav) );
861 /***********************************************************************
863 ***********************************************************************
865 **********************************************************************/
866 static void hb_dvdnav_stop( hb_dvd_t * e )
870 /***********************************************************************
872 ***********************************************************************
874 **********************************************************************/
875 static int hb_dvdnav_seek( hb_dvd_t * e, float f )
877 hb_dvdnav_t * d = &(e->dvdnav);
878 uint64_t sector = f * d->title_block_count;
879 int result, event, len;
880 uint8_t buf[HB_DVD_READ_BUFFER_SIZE];
888 // XXX the current version of libdvdnav can't seek outside the current
889 // PGC. Check if the place we're seeking to is in a different
890 // PGC. Position there & adjust the offset if so.
891 hb_chapter_t *pgc_change = hb_list_item(d->list_chapter, 0 );
892 for ( ii = 0; ii < hb_list_count( d->list_chapter ); ++ii )
894 hb_chapter_t *chapter = hb_list_item( d->list_chapter, ii );
896 if ( chapter->pgcn != pgc_change->pgcn )
898 // this chapter's in a different pgc from the previous - note the
899 // change so we can make sector offset's be pgc relative.
900 pgc_change = chapter;
902 if ( chapter->block_start <= sector && sector <= chapter->block_end )
904 // this chapter contains the sector we want - see if it's in a
905 // different pgc than the one we're currently in.
906 int32_t title, pgcn, pgn;
907 if (dvdnav_current_title_program( d->dvdnav, &title, &pgcn, &pgn ) != DVDNAV_STATUS_OK)
908 hb_log("dvdnav cur pgcn err: %s", dvdnav_err_to_string(d->dvdnav));
909 if ( d->title != title || chapter->pgcn != pgcn )
911 // this chapter is in a different pgc - switch to it.
912 if (dvdnav_program_play(d->dvdnav, d->title, chapter->pgcn, chapter->pgn) != DVDNAV_STATUS_OK)
913 hb_log("dvdnav prog play err: %s", dvdnav_err_to_string(d->dvdnav));
915 // seek sectors are pgc-relative so remove the pgc start sector.
916 sector -= pgc_change->block_start;
921 // dvdnav will not let you seek or poll current position
922 // till it reaches a certain point in parsing. so we
923 // have to get blocks until we reach a cell
924 // Put an arbitrary limit of 100 blocks on how long we search
925 for (ii = 0; ii < 100 && !done; ii++)
927 result = dvdnav_get_next_block( d->dvdnav, buf, &event, &len );
928 if ( result == DVDNAV_STATUS_ERR )
930 hb_log("dvdnav: Read Error, %s", dvdnav_err_to_string(d->dvdnav));
935 case DVDNAV_BLOCK_OK:
936 case DVDNAV_CELL_CHANGE:
940 case DVDNAV_STILL_FRAME:
941 dvdnav_still_skip( d->dvdnav );
945 dvdnav_wait_skip( d->dvdnav );
949 hb_log("dvdnav: stop encountered during seek");
953 case DVDNAV_HOP_CHANNEL:
954 case DVDNAV_NAV_PACKET:
955 case DVDNAV_VTS_CHANGE:
956 case DVDNAV_HIGHLIGHT:
957 case DVDNAV_AUDIO_STREAM_CHANGE:
958 case DVDNAV_SPU_STREAM_CHANGE:
959 case DVDNAV_SPU_CLUT_CHANGE:
966 if (dvdnav_sector_search(d->dvdnav, sector, SEEK_SET) != DVDNAV_STATUS_OK)
968 hb_error( "dvd: dvdnav_sector_search failed - %s",
969 dvdnav_err_to_string(d->dvdnav) );
975 /***********************************************************************
977 ***********************************************************************
979 **********************************************************************/
980 static int hb_dvdnav_read( hb_dvd_t * e, hb_buffer_t * b )
982 hb_dvdnav_t * d = &(e->dvdnav);
983 int result, event, len;
992 result = dvdnav_get_next_block( d->dvdnav, b->data, &event, &len );
993 if ( result == DVDNAV_STATUS_ERR )
995 hb_log("dvdnav: Read Error, %s", dvdnav_err_to_string(d->dvdnav));
1000 case DVDNAV_BLOCK_OK:
1001 // We have received a regular block of the currently playing
1004 // The muxers expect to only get chapter 2 and above
1005 // They write chapter 1 when chapter 2 is detected.
1007 b->new_chap = chapter;
1013 * Nothing to do here.
1017 case DVDNAV_STILL_FRAME:
1019 * We have reached a still frame. A real player application
1020 * would wait the amount of time specified by the still's
1021 * length while still handling user input to make menus and
1022 * other interactive stills work. A length of 0xff means an
1023 * indefinite still which has to be skipped indirectly by some
1026 dvdnav_still_skip( d->dvdnav );
1031 * We have reached a point in DVD playback, where timing is
1032 * critical. Player application with internal fifos can
1033 * introduce state inconsistencies, because libdvdnav is
1034 * always the fifo's length ahead in the stream compared to
1035 * what the application sees. Such applications should wait
1036 * until their fifos are empty when they receive this type of
1039 dvdnav_wait_skip( d->dvdnav );
1042 case DVDNAV_SPU_CLUT_CHANGE:
1044 * Player applications should pass the new colour lookup table
1045 * to their SPU decoder
1049 case DVDNAV_SPU_STREAM_CHANGE:
1051 * Player applications should inform their SPU decoder to
1056 case DVDNAV_AUDIO_STREAM_CHANGE:
1058 * Player applications should inform their audio decoder to
1063 case DVDNAV_HIGHLIGHT:
1065 * Player applications should inform their overlay engine to
1066 * highlight the given button
1070 case DVDNAV_VTS_CHANGE:
1072 * Some status information like video aspect and video scale
1073 * permissions do not change inside a VTS. Therefore this
1074 * event can be used to query such information only when
1075 * necessary and update the decoding/displaying accordingly.
1079 case DVDNAV_CELL_CHANGE:
1081 * Some status information like the current Title and Part
1082 * numbers do not change inside a cell. Therefore this event
1083 * can be used to query such information only when necessary
1084 * and update the decoding/displaying accordingly.
1087 int tt = 0, pgcn = 0, pgn = 0, c;
1089 dvdnav_current_title_program(d->dvdnav, &tt, &pgcn, &pgn);
1092 // Transition to another title signals that we are done.
1095 c = FindChapterIndex(d->list_chapter, pgcn, pgn);
1096 if (c != d->chapter)
1100 // Some titles end with a 'link' back to the beginning so
1101 // a transition to an earlier chapter means we're done.
1104 chapter = d->chapter = c;
1109 case DVDNAV_NAV_PACKET:
1111 * A NAV packet provides PTS discontinuity information, angle
1112 * linking information and button definitions for DVD menus.
1113 * Angles are handled completely inside libdvdnav. For the
1114 * menus to work, the NAV packet information has to be passed
1115 * to the overlay engine of the player so that it knows the
1116 * dimensions of the button areas.
1119 // mpegdemux expects to get these. I don't think it does
1120 // anything useful with them however.
1122 // The muxers expect to only get chapter 2 and above
1123 // They write chapter 1 when chapter 2 is detected.
1125 b->new_chap = chapter;
1131 case DVDNAV_HOP_CHANNEL:
1133 * This event is issued whenever a non-seamless operation has
1134 * been executed. Applications with fifos should drop the
1135 * fifos content to speed up responsiveness.
1141 * Playback should end here.
1153 /***********************************************************************
1155 ***********************************************************************
1156 * Returns in which chapter the next block to be read is.
1157 * Chapter numbers start at 1.
1158 **********************************************************************/
1159 static int hb_dvdnav_chapter( hb_dvd_t * e )
1161 hb_dvdnav_t * d = &(e->dvdnav);
1162 int32_t t, pgcn, pgn;
1165 if (dvdnav_current_title_program(d->dvdnav, &t, &pgcn, &pgn) != DVDNAV_STATUS_OK)
1169 c = FindChapterIndex( d->list_chapter, pgcn, pgn );
1173 /***********************************************************************
1175 ***********************************************************************
1176 * Closes and frees everything
1177 **********************************************************************/
1178 static void hb_dvdnav_close( hb_dvd_t ** _d )
1180 hb_dvdnav_t * d = &((*_d)->dvdnav);
1182 if( d->dvdnav ) dvdnav_close( d->dvdnav );
1183 if( d->vmg ) ifoClose( d->vmg );
1184 if( d->reader ) DVDClose( d->reader );
1190 /***********************************************************************
1191 * hb_dvdnav_angle_count
1192 ***********************************************************************
1193 * Returns the number of angles supported.
1194 **********************************************************************/
1195 static int hb_dvdnav_angle_count( hb_dvd_t * e )
1197 hb_dvdnav_t * d = &(e->dvdnav);
1198 int current, angle_count;
1200 if (dvdnav_get_angle_info( d->dvdnav, ¤t, &angle_count) != DVDNAV_STATUS_OK)
1202 hb_log("dvdnav_get_angle_info %s", dvdnav_err_to_string(d->dvdnav));
1208 /***********************************************************************
1209 * hb_dvdnav_set_angle
1210 ***********************************************************************
1211 * Sets the angle to read
1212 **********************************************************************/
1213 static void hb_dvdnav_set_angle( hb_dvd_t * e, int angle )
1215 hb_dvdnav_t * d = &(e->dvdnav);
1217 if (dvdnav_angle_change( d->dvdnav, angle) != DVDNAV_STATUS_OK)
1219 hb_log("dvdnav_angle_change %s", dvdnav_err_to_string(d->dvdnav));
1223 /***********************************************************************
1225 ***********************************************************************
1226 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1227 * cell to be read when we will be done with cell_cur.
1228 **********************************************************************/
1229 static int FindChapterIndex( hb_list_t * list, int pgcn, int pgn )
1232 hb_chapter_t *chapter;
1234 count = hb_list_count( list );
1235 for (ii = 0; ii < count; ii++)
1237 chapter = hb_list_item( list, ii );
1238 if (chapter->pgcn == pgcn && chapter->pgn == pgn)
1239 return chapter->index;
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 **********************************************************************/
1250 static int FindNextCell( pgc_t *pgc, int cell_cur )
1255 if( pgc->cell_playback[cell_cur].block_type ==
1256 BLOCK_TYPE_ANGLE_BLOCK )
1259 while( pgc->cell_playback[cell_cur+i].block_mode !=
1260 BLOCK_MODE_LAST_CELL )
1264 cell_next = cell_cur + i + 1;
1265 hb_log( "dvd: Skipping multi-angle cells %d-%d",
1271 cell_next = cell_cur + 1;
1276 /***********************************************************************
1278 ***********************************************************************
1279 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1280 * cell to be read when we will be done with cell_cur.
1281 * Since pg chains can be circularly linked (either from a read error or
1282 * deliberately) pgcn_map tracks program chains we've already seen.
1283 **********************************************************************/
1284 static int NextPgcn( ifo_handle_t *ifo, int pgcn, uint32_t pgcn_map[MAX_PGCN/32] )
1289 pgcn_map[pgcn >> 5] |= (1 << (pgcn & 31));
1291 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
1292 next_pgcn = pgc->next_pgc_nr;
1293 if ( next_pgcn < 1 || next_pgcn >= MAX_PGCN || next_pgcn > ifo->vts_pgcit->nr_of_pgci_srp )
1296 return pgcn_map[next_pgcn >> 5] & (1 << (next_pgcn & 31))? 0 : next_pgcn;
1299 /***********************************************************************
1301 ***********************************************************************
1302 * Pgc links can loop. I track which have been visited in a bit vector
1303 * Initialize the bit vector to empty.
1304 **********************************************************************/
1305 static void PgcWalkInit( uint32_t pgcn_map[MAX_PGCN/32] )
1307 memset(pgcn_map, 0, sizeof(pgcn_map) );
1310 /***********************************************************************
1312 ***********************************************************************
1314 **********************************************************************/
1315 static int dvdtime2msec(dvd_time_t * dt)
1317 double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
1318 double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
1320 ms = (((dt->hour & 0xf0) >> 3) * 5 + (dt->hour & 0x0f)) * 3600000;
1321 ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
1322 ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
1326 ms += ((dt->frame_u & 0x30) >> 3) * 5 +
1327 (dt->frame_u & 0x0f) * 1000.0 / fps;