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 /***********************************************************************
48 **********************************************************************/
49 static void PgcWalkInit( void );
50 static int FindChapterIndex( hb_list_t * list, int pgcn, int pgn );
51 static int NextPgcn( ifo_handle_t *ifo, int pgcn );
52 static int FindNextCell( pgc_t *pgc, int cell_cur );
53 static int dvdtime2msec( dvd_time_t * );
55 hb_dvd_func_t * hb_dvdnav_methods( void )
57 return &hb_dvdnav_func;
60 static char * hb_dvdnav_name( char * path )
62 static char name[1024];
63 unsigned char unused[1024];
64 dvd_reader_t * reader;
66 reader = DVDOpen( path );
72 if( DVDUDFVolumeInfo( reader, name, sizeof( name ),
73 unused, sizeof( unused ) ) )
83 /***********************************************************************
85 ***********************************************************************
86 * Once dvdnav has entered the 'stopped' state, it can not be revived
87 * dvdnav_reset doesn't work because it doesn't remember the path
88 * So this function re-opens dvdnav
89 **********************************************************************/
90 static int hb_dvdnav_reset( hb_dvdnav_t * d )
93 dvdnav_close( d->dvdnav );
96 if( dvdnav_open(&d->dvdnav, d->path) != DVDNAV_STATUS_OK )
99 * Not an error, may be a stream - which we'll try in a moment.
101 hb_log( "dvd: not a dvd - trying as a stream/file instead" );
105 if (dvdnav_set_readahead_flag(d->dvdnav, DVD_READ_CACHE) !=
108 hb_error("Error: dvdnav_set_readahead_flag: %s\n",
109 dvdnav_err_to_string(d->dvdnav));
114 ** set the PGC positioning flag to have position information
115 ** relatively to the whole feature instead of just relatively to the
118 if (dvdnav_set_PGC_positioning_flag(d->dvdnav, 1) != DVDNAV_STATUS_OK)
120 hb_error("Error: dvdnav_set_PGC_positioning_flag: %s\n",
121 dvdnav_err_to_string(d->dvdnav));
127 if( d->dvdnav ) dvdnav_close( d->dvdnav );
131 /***********************************************************************
133 ***********************************************************************
135 **********************************************************************/
136 static hb_dvd_t * hb_dvdnav_init( char * path )
141 e = calloc( sizeof( hb_dvd_t ), 1 );
145 if( dvdnav_open(&d->dvdnav, path) != DVDNAV_STATUS_OK )
148 * Not an error, may be a stream - which we'll try in a moment.
150 hb_log( "dvd: not a dvd - trying as a stream/file instead" );
154 if (dvdnav_set_readahead_flag(d->dvdnav, DVD_READ_CACHE) !=
157 hb_error("Error: dvdnav_set_readahead_flag: %s\n",
158 dvdnav_err_to_string(d->dvdnav));
163 ** set the PGC positioning flag to have position information
164 ** relatively to the whole feature instead of just relatively to the
167 if (dvdnav_set_PGC_positioning_flag(d->dvdnav, 1) != DVDNAV_STATUS_OK)
169 hb_error("Error: dvdnav_set_PGC_positioning_flag: %s\n",
170 dvdnav_err_to_string(d->dvdnav));
175 if( !( d->reader = DVDOpen( path ) ) )
178 * Not an error, may be a stream - which we'll try in a moment.
180 hb_log( "dvd: not a dvd - trying as a stream/file instead" );
185 if( !( d->vmg = ifoOpen( d->reader, 0 ) ) )
187 hb_error( "dvd: ifoOpen failed" );
191 d->path = strdup( path );
196 if( d->dvdnav ) dvdnav_close( d->dvdnav );
197 if( d->vmg ) ifoClose( d->vmg );
198 if( d->reader ) DVDClose( d->reader );
203 /***********************************************************************
204 * hb_dvdnav_title_count
205 **********************************************************************/
206 static int hb_dvdnav_title_count( hb_dvd_t * e )
209 hb_dvdnav_t * d = &(e->dvdnav);
211 dvdnav_get_number_of_titles(d->dvdnav, &titles);
216 PttDuration(ifo_handle_t *ifo, int ttn, int pttn, int *blocks, int *last_pgcn)
220 uint64_t duration = 0;
221 int cell_start, cell_end;
224 // Initialize map of visited pgc's to prevent loops
226 pgcn = ifo->vts_ptt_srpt->title[ttn-1].ptt[pttn-1].pgcn;
227 pgn = ifo->vts_ptt_srpt->title[ttn-1].ptt[pttn-1].pgn;
228 if ( pgcn < 1 || pgcn > ifo->vts_pgcit->nr_of_pgci_srp )
230 hb_error( "invalid PGC ID %d, skipping", pgcn );
234 if( pgn <= 0 || pgn > 99 )
236 hb_error( "scan: pgn %d not valid, skipping", pgn );
243 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
246 hb_error( "scan: pgc not valid, skipping" );
249 duration += 90LL * dvdtime2msec( &pgc->playback_time );
251 cell_start = pgc->program_map[pgn-1] - 1;
252 cell_end = pgc->nr_of_cells - 1;
253 for(i = cell_start; i <= cell_end; i = FindNextCell(pgc, i))
255 *blocks += pgc->cell_playback[i].last_sector + 1 -
256 pgc->cell_playback[i].first_sector;
260 } while((pgcn = NextPgcn(ifo, pgcn)) != 0);
264 /***********************************************************************
265 * hb_dvdnav_title_scan
266 **********************************************************************/
267 static hb_title_t * hb_dvdnav_title_scan( hb_dvd_t * e, int t )
270 hb_dvdnav_t * d = &(e->dvdnav);
272 ifo_handle_t * ifo = NULL;
273 int pgcn, pgn, pgcn_end, i, c;
277 hb_chapter_t * chapter;
279 uint64_t duration, longest;
280 int longest_pgcn, longest_pgn, longest_pgcn_end;
281 float duration_correction;
284 hb_log( "scan: scanning title %d", t );
286 title = hb_title_init( d->path, t );
287 if (dvdnav_get_title_string(d->dvdnav, &name) == DVDNAV_STATUS_OK)
289 strncpy( title->name, name, sizeof( title->name ) );
293 char * p_cur, * p_last = d->path;
294 for( p_cur = d->path; *p_cur; p_cur++ )
296 if( p_cur[0] == '/' && p_cur[1] )
301 snprintf( title->name, sizeof( title->name ), "%s", p_last );
304 /* VTS which our title is in */
305 title->vts = d->vmg->tt_srpt->title[t-1].title_set_nr;
309 /* A VTS of 0 means the title wasn't found in the title set */
310 hb_error("Invalid VTS (title set) number: %i", title->vts);
314 hb_log( "scan: opening IFO for VTS %d", title->vts );
315 if( !( ifo = ifoOpen( d->reader, title->vts ) ) )
317 hb_error( "scan: ifoOpen failed" );
321 /* ignore titles with bogus cell addresses so we don't abort later
323 for ( i = 0; i < ifo->vts_c_adt->nr_of_vobs; ++i)
325 if( (ifo->vts_c_adt->cell_adr_table[i].start_sector & 0xffffff ) ==
328 hb_error( "scan: cell_adr_table[%d].start_sector invalid (0x%x) "
329 "- skipping title", i,
330 ifo->vts_c_adt->cell_adr_table[i].start_sector );
333 if( (ifo->vts_c_adt->cell_adr_table[i].last_sector & 0xffffff ) ==
336 hb_error( "scan: cell_adr_table[%d].last_sector invalid (0x%x) "
337 "- skipping title", i,
338 ifo->vts_c_adt->cell_adr_table[i].last_sector );
341 if( ifo->vts_c_adt->cell_adr_table[i].start_sector >=
342 ifo->vts_c_adt->cell_adr_table[i].last_sector )
344 hb_error( "scan: cell_adr_table[%d].start_sector (0x%x) "
345 "is not before last_sector (0x%x) - skipping title", i,
346 ifo->vts_c_adt->cell_adr_table[i].start_sector,
347 ifo->vts_c_adt->cell_adr_table[i].last_sector );
352 if( global_verbosity_level == 3 )
354 ifo_print( d->reader, title->vts );
357 /* Position of the title in the VTS */
358 title->ttn = d->vmg->tt_srpt->title[t-1].vts_ttn;
359 if ( title->ttn < 1 || title->ttn > ifo->vts_ptt_srpt->nr_of_srpts )
361 hb_error( "invalid VTS PTT offset %d for title %d, skipping", title->ttn, t );
368 longest_pgcn_end = -1;
370 for( i = 0; i < ifo->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts; i++ )
374 duration = PttDuration(ifo, title->ttn, i+1, &blocks, &pgcn_end);
375 pgcn = ifo->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgcn;
376 pgn = ifo->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgn;
377 if( duration > longest )
381 longest_pgcn_end = pgcn_end;
383 title->block_count = blocks;
385 else if (pgcn == longest_pgcn && pgn < longest_pgn)
388 title->block_count = blocks;
392 /* ignore titles under 10 seconds because they're often stills or
393 * clips with no audio & our preview code doesn't currently handle
394 * either of these. */
395 if( longest < 900000LL )
397 hb_log( "scan: ignoring title (too short)" );
402 pgcn_end = longest_pgcn_end;
408 if ( pgcn < 1 || pgcn > ifo->vts_pgcit->nr_of_pgci_srp )
410 hb_error( "invalid PGC ID %d for title %d, skipping", pgcn, t );
414 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
416 hb_log("pgc_id: %d, pgn: %d: pgc: 0x%x", pgcn, pgn, pgc);
419 title->cell_start = pgc->program_map[pgn-1] - 1;
420 title->block_start = pgc->cell_playback[title->cell_start].first_sector;
422 pgc = ifo->vts_pgcit->pgci_srp[pgcn_end-1].pgc;
425 title->cell_end = pgc->nr_of_cells - 1;
426 title->block_end = pgc->cell_playback[title->cell_end].last_sector;
428 hb_log( "scan: vts=%d, ttn=%d, cells=%d->%d, blocks=%d->%d, "
429 "%d blocks", title->vts, title->ttn, title->cell_start,
430 title->cell_end, title->block_start, title->block_end,
431 title->block_count );
434 title->duration = longest;
435 title->hours = title->duration / 90000 / 3600;
436 title->minutes = ( ( title->duration / 90000 ) % 3600 ) / 60;
437 title->seconds = ( title->duration / 90000 ) % 60;
438 hb_log( "scan: duration is %02d:%02d:%02d (%lld ms)",
439 title->hours, title->minutes, title->seconds,
440 title->duration / 90 );
442 /* Detect languages */
443 for( i = 0; i < ifo->vtsi_mat->nr_of_vts_audio_streams; i++ )
445 hb_audio_t * audio, * audio_tmp;
446 int audio_format, lang_code, audio_control,
448 iso639_lang_t * lang;
449 int lang_extension = 0;
451 hb_log( "scan: checking audio %d", i + 1 );
453 audio = calloc( sizeof( hb_audio_t ), 1 );
455 audio_format = ifo->vtsi_mat->vts_audio_attr[i].audio_format;
456 lang_code = ifo->vtsi_mat->vts_audio_attr[i].lang_code;
457 lang_extension = ifo->vtsi_mat->vts_audio_attr[i].code_extension;
459 ifo->vts_pgcit->pgci_srp[title_pgcn-1].pgc->audio_control[i];
461 if( !( audio_control & 0x8000 ) )
463 hb_log( "scan: audio channel is not active" );
468 position = ( audio_control & 0x7F00 ) >> 8;
470 switch( audio_format )
473 audio->id = ( ( 0x80 + position ) << 8 ) | 0xbd;
474 audio->config.in.codec = HB_ACODEC_AC3;
479 audio->id = 0xc0 + position;
480 audio->config.in.codec = HB_ACODEC_MPGA;
484 audio->id = ( ( 0xa0 + position ) << 8 ) | 0xbd;
485 audio->config.in.codec = HB_ACODEC_LPCM;
489 audio->id = ( ( 0x88 + position ) << 8 ) | 0xbd;
490 audio->config.in.codec = HB_ACODEC_DCA;
495 audio->config.in.codec = 0;
496 hb_log( "scan: unknown audio codec (%x)",
505 /* Check for duplicate tracks */
507 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
509 audio_tmp = hb_list_item( title->list_audio, j );
510 if( audio->id == audio_tmp->id )
518 hb_log( "scan: duplicate audio track" );
523 audio->config.lang.type = lang_extension;
525 lang = lang_for_code( ifo->vtsi_mat->vts_audio_attr[i].lang_code );
527 snprintf( audio->config.lang.description, sizeof( audio->config.lang.description ), "%s (%s)",
528 strlen(lang->native_name) ? lang->native_name : lang->eng_name,
529 audio->config.in.codec == HB_ACODEC_AC3 ? "AC3" : ( audio->config.in.codec ==
530 HB_ACODEC_DCA ? "DTS" : ( audio->config.in.codec ==
531 HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) ) );
532 snprintf( audio->config.lang.simple, sizeof( audio->config.lang.simple ), "%s",
533 strlen(lang->native_name) ? lang->native_name : lang->eng_name );
534 snprintf( audio->config.lang.iso639_2, sizeof( audio->config.lang.iso639_2 ), "%s",
537 switch( lang_extension )
543 strcat( audio->config.lang.description, " (Visually Impaired)" );
546 strcat( audio->config.lang.description, " (Director's Commentary 1)" );
549 strcat( audio->config.lang.description, " (Director's Commentary 2)" );
555 hb_log( "scan: id=%x, lang=%s, 3cc=%s ext=%i", audio->id,
556 audio->config.lang.description, audio->config.lang.iso639_2,
559 audio->config.in.track = i;
560 hb_list_add( title->list_audio, audio );
563 if( !hb_list_count( title->list_audio ) )
565 hb_log( "scan: ignoring title (no audio track)" );
569 memcpy( title->palette,
570 ifo->vts_pgcit->pgci_srp[title_pgcn-1].pgc->palette,
571 16 * sizeof( uint32_t ) );
573 /* Check for subtitles */
574 for( i = 0; i < ifo->vtsi_mat->nr_of_vts_subp_streams; i++ )
576 hb_subtitle_t * subtitle;
579 iso639_lang_t * lang;
580 int lang_extension = 0;
582 hb_log( "scan: checking subtitle %d", i + 1 );
585 ifo->vts_pgcit->pgci_srp[title_pgcn-1].pgc->subp_control[i];
587 if( !( spu_control & 0x80000000 ) )
589 hb_log( "scan: subtitle channel is not active" );
593 if( ifo->vtsi_mat->vts_video_attr.display_aspect_ratio )
595 switch( ifo->vtsi_mat->vts_video_attr.permitted_df )
598 position = spu_control & 0xFF;
601 position = ( spu_control >> 8 ) & 0xFF;
604 position = ( spu_control >> 16 ) & 0xFF;
609 position = ( spu_control >> 24 ) & 0x7F;
612 lang_extension = ifo->vtsi_mat->vts_subp_attr[i].code_extension;
614 lang = lang_for_code( ifo->vtsi_mat->vts_subp_attr[i].lang_code );
616 subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
617 subtitle->id = ( ( 0x20 + position ) << 8 ) | 0xbd;
618 snprintf( subtitle->lang, sizeof( subtitle->lang ), "%s",
619 strlen(lang->native_name) ? lang->native_name : lang->eng_name);
620 snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ), "%s",
623 subtitle->type = lang_extension;
625 switch( lang_extension )
632 strcat( subtitle->lang, " (Caption with bigger size character)");
635 strcat( subtitle->lang, " (Caption for Children)");
640 strcat( subtitle->lang, " (Closed Caption)");
643 strcat( subtitle->lang, " (Closed Caption with bigger size character)");
646 strcat( subtitle->lang, " (Closed Caption for Children)");
651 strcat( subtitle->lang, " (Forced Caption)");
660 strcat( subtitle->lang, " (Director's Commentary)");
663 strcat( subtitle->lang, " (Director's Commentary with bigger size character)");
666 strcat( subtitle->lang, " (Director's Commentary for Children)");
671 hb_log( "scan: id=%x, lang=%s, 3cc=%s", subtitle->id,
672 subtitle->lang, subtitle->iso639_2 );
674 hb_list_add( title->list_subtitle, subtitle );
682 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
684 for (i = pgn; i <= pgc->nr_of_programs; i++)
686 chapter = calloc( sizeof( hb_chapter_t ), 1 );
688 chapter->index = c + 1;
689 chapter->pgcn = pgcn;
691 hb_list_add( title->list_chapter, chapter );
696 } while ((pgcn = NextPgcn(ifo, pgcn)) != 0);
698 hb_log( "scan: title %d has %d chapters", t, c );
701 count = hb_list_count( title->list_chapter );
702 for (i = 0; i < count; i++)
704 int pgcn_next, pgn_next;
706 hb_chapter_t *chapter_next;
709 chapter = hb_list_item( title->list_chapter, i );
711 pgcn = chapter->pgcn;
713 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
716 chapter->cell_start = pgc->program_map[pgn-1] - 1;
717 chapter->block_start =
718 pgc->cell_playback[chapter->cell_start].first_sector;
719 pgc_cell_end = pgc->nr_of_cells - 1;
724 /* The cell before the starting cell of the next chapter,
726 chapter_next = hb_list_item( title->list_chapter, i+1 );
727 pgcn_next = chapter_next->pgcn;
728 pgn_next = chapter_next->pgn;
729 pgc_next = ifo->vts_pgcit->pgci_srp[pgcn_next-1].pgc;
730 if (pgcn_next == pgcn)
732 chapter->cell_end = pgc_next->program_map[pgn_next-1] - 2;
736 chapter->cell_end = pgc_cell_end;
738 if( chapter->cell_end < 0 )
747 /* ... the last cell of the title */
748 chapter->cell_end = pgc_cell_end;
751 pgc->cell_playback[chapter->cell_end].last_sector;
753 /* Block count, duration */
754 chapter->block_count = 0;
755 chapter->duration = 0;
757 cell_cur = chapter->cell_start;
758 while( cell_cur <= chapter->cell_end )
760 #define cp pgc->cell_playback[cell_cur]
761 chapter->block_count += cp.last_sector + 1 - cp.first_sector;
762 chapter->duration += 90LL * dvdtime2msec( &cp.playback_time );
764 cell_cur = FindNextCell( pgc, cell_cur );
766 duration += chapter->duration;
769 /* The durations we get for chapters aren't precise. Scale them so
770 the total matches the title duration */
771 duration_correction = (float) title->duration / (float) duration;
772 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
775 chapter = hb_list_item( title->list_chapter, i );
776 chapter->duration = duration_correction * chapter->duration;
777 seconds = ( chapter->duration + 45000 ) / 90000;
778 chapter->hours = seconds / 3600;
779 chapter->minutes = ( seconds % 3600 ) / 60;
780 chapter->seconds = seconds % 60;
782 hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %lld ms",
783 chapter->index, chapter->cell_start, chapter->cell_end,
784 chapter->block_start, chapter->block_end,
785 chapter->block_count, chapter->duration / 90 );
788 /* Get aspect. We don't get width/height/rate infos here as
789 they tend to be wrong */
790 switch( ifo->vtsi_mat->vts_video_attr.display_aspect_ratio )
793 title->container_aspect = 4. / 3.;
796 title->container_aspect = 16. / 9.;
799 hb_log( "scan: unknown aspect" );
803 hb_log( "scan: aspect = %g", title->aspect );
805 /* This title is ok so far */
809 hb_list_close( &title->list_audio );
814 if( ifo ) ifoClose( ifo );
819 /***********************************************************************
821 ***********************************************************************
822 * Title and chapter start at 1
823 **********************************************************************/
824 static int hb_dvdnav_start( hb_dvd_t * e, hb_title_t *title, int c )
826 hb_dvdnav_t * d = &(e->dvdnav);
827 int t = title->index;
828 hb_chapter_t *chapter;
829 dvdnav_status_t result;
831 d->title_block_count = title->block_count;
832 d->list_chapter = title->list_chapter;
834 if ( d->stopped && !hb_dvdnav_reset(d) )
838 chapter = hb_list_item( title->list_chapter, c - 1);
840 result = dvdnav_program_play(d->dvdnav, t, chapter->pgcn, chapter->pgn);
842 result = dvdnav_part_play(d->dvdnav, t, 1);
843 if (result != DVDNAV_STATUS_OK)
845 hb_error( "dvd: dvdnav_*_play failed - %s",
846 dvdnav_err_to_string(d->dvdnav) );
854 /***********************************************************************
856 ***********************************************************************
858 **********************************************************************/
859 static void hb_dvdnav_stop( hb_dvd_t * e )
863 /***********************************************************************
865 ***********************************************************************
867 **********************************************************************/
868 static int hb_dvdnav_seek( hb_dvd_t * e, float f )
870 hb_dvdnav_t * d = &(e->dvdnav);
872 int result, event, len;
873 uint8_t buf[HB_DVD_READ_BUFFER_SIZE];
881 // dvdnav will not let you seek or poll current position
882 // till it reaches a certain point in parsing. so we
883 // have to get blocks until we reach a cell
884 // Put an arbitrary limit of 100 blocks on how long we search
885 for (ii = 0; ii < 100 && !done; ii++)
887 result = dvdnav_get_next_block( d->dvdnav, buf, &event, &len );
888 if ( result == DVDNAV_STATUS_ERR )
890 hb_log("dvdnav: Read Error, %s", dvdnav_err_to_string(d->dvdnav));
895 case DVDNAV_BLOCK_OK:
896 case DVDNAV_CELL_CHANGE:
899 case DVDNAV_STILL_FRAME:
900 dvdnav_still_skip( d->dvdnav );
904 dvdnav_wait_skip( d->dvdnav );
908 hb_log("dvdnav: stop encountered during seek");
912 case DVDNAV_HOP_CHANNEL:
913 case DVDNAV_NAV_PACKET:
914 case DVDNAV_VTS_CHANGE:
915 case DVDNAV_HIGHLIGHT:
916 case DVDNAV_AUDIO_STREAM_CHANGE:
917 case DVDNAV_SPU_STREAM_CHANGE:
918 case DVDNAV_SPU_CLUT_CHANGE:
923 } while (!(event == DVDNAV_CELL_CHANGE || event == DVDNAV_BLOCK_OK));
925 sector = f * d->title_block_count;
926 if (dvdnav_sector_search(d->dvdnav, sector, SEEK_SET) != DVDNAV_STATUS_OK)
928 hb_error( "dvd: dvdnav_sector_search failed - %s",
929 dvdnav_err_to_string(d->dvdnav) );
935 /***********************************************************************
937 ***********************************************************************
939 **********************************************************************/
940 static int hb_dvdnav_read( hb_dvd_t * e, hb_buffer_t * b )
942 hb_dvdnav_t * d = &(e->dvdnav);
943 int result, event, len;
952 result = dvdnav_get_next_block( d->dvdnav, b->data, &event, &len );
953 if ( result == DVDNAV_STATUS_ERR )
955 hb_log("dvdnav: Read Error, %s", dvdnav_err_to_string(d->dvdnav));
960 case DVDNAV_BLOCK_OK:
961 // We have received a regular block of the currently playing
964 // The muxers expect to only get chapter 2 and above
965 // They write chapter 1 when chapter 2 is detected.
967 b->new_chap = chapter;
973 * Nothing to do here.
977 case DVDNAV_STILL_FRAME:
979 * We have reached a still frame. A real player application
980 * would wait the amount of time specified by the still's
981 * length while still handling user input to make menus and
982 * other interactive stills work. A length of 0xff means an
983 * indefinite still which has to be skipped indirectly by some
986 dvdnav_still_skip( d->dvdnav );
991 * We have reached a point in DVD playback, where timing is
992 * critical. Player application with internal fifos can
993 * introduce state inconsistencies, because libdvdnav is
994 * always the fifo's length ahead in the stream compared to
995 * what the application sees. Such applications should wait
996 * until their fifos are empty when they receive this type of
999 dvdnav_wait_skip( d->dvdnav );
1002 case DVDNAV_SPU_CLUT_CHANGE:
1004 * Player applications should pass the new colour lookup table
1005 * to their SPU decoder
1009 case DVDNAV_SPU_STREAM_CHANGE:
1011 * Player applications should inform their SPU decoder to
1016 case DVDNAV_AUDIO_STREAM_CHANGE:
1018 * Player applications should inform their audio decoder to
1023 case DVDNAV_HIGHLIGHT:
1025 * Player applications should inform their overlay engine to
1026 * highlight the given button
1030 case DVDNAV_VTS_CHANGE:
1032 * Some status information like video aspect and video scale
1033 * permissions do not change inside a VTS. Therefore this
1034 * event can be used to query such information only when
1035 * necessary and update the decoding/displaying accordingly.
1039 case DVDNAV_CELL_CHANGE:
1041 * Some status information like the current Title and Part
1042 * numbers do not change inside a cell. Therefore this event
1043 * can be used to query such information only when necessary
1044 * and update the decoding/displaying accordingly.
1047 int tt = 0, pgcn = 0, pgn = 0, c;
1049 dvdnav_current_title_program(d->dvdnav, &tt, &pgcn, &pgn);
1052 // Transition to another title signals that we are done.
1055 c = FindChapterIndex(d->list_chapter, pgcn, pgn);
1057 chapter = d->chapter = c;
1061 case DVDNAV_NAV_PACKET:
1063 * A NAV packet provides PTS discontinuity information, angle
1064 * linking information and button definitions for DVD menus.
1065 * Angles are handled completely inside libdvdnav. For the
1066 * menus to work, the NAV packet information has to be passed
1067 * to the overlay engine of the player so that it knows the
1068 * dimensions of the button areas.
1071 // mpegdemux expects to get these. I don't think it does
1072 // anything useful with them however.
1074 // The muxers expect to only get chapter 2 and above
1075 // They write chapter 1 when chapter 2 is detected.
1077 b->new_chap = chapter;
1083 case DVDNAV_HOP_CHANNEL:
1085 * This event is issued whenever a non-seamless operation has
1086 * been executed. Applications with fifos should drop the
1087 * fifos content to speed up responsiveness.
1093 * Playback should end here.
1105 /***********************************************************************
1107 ***********************************************************************
1108 * Returns in which chapter the next block to be read is.
1109 * Chapter numbers start at 1.
1110 **********************************************************************/
1111 static int hb_dvdnav_chapter( hb_dvd_t * e )
1113 hb_dvdnav_t * d = &(e->dvdnav);
1114 int32_t t, pgcn, pgn;
1117 if (dvdnav_current_title_program(d->dvdnav, &t, &pgcn, &pgn) != DVDNAV_STATUS_OK)
1121 c = FindChapterIndex( d->list_chapter, pgcn, pgn );
1125 /***********************************************************************
1127 ***********************************************************************
1128 * Closes and frees everything
1129 **********************************************************************/
1130 static void hb_dvdnav_close( hb_dvd_t ** _d )
1132 hb_dvdnav_t * d = &((*_d)->dvdnav);
1134 if( d->dvdnav ) dvdnav_close( d->dvdnav );
1135 if( d->vmg ) ifoClose( d->vmg );
1136 if( d->reader ) DVDClose( d->reader );
1142 /***********************************************************************
1143 * hb_dvdnav_angle_count
1144 ***********************************************************************
1145 * Returns the number of angles supported.
1146 **********************************************************************/
1147 static int hb_dvdnav_angle_count( hb_dvd_t * e )
1149 hb_dvdnav_t * d = &(e->dvdnav);
1150 int current, angle_count;
1152 if (dvdnav_get_angle_info( d->dvdnav, ¤t, &angle_count) != DVDNAV_STATUS_OK)
1154 hb_log("dvdnav_get_angle_info %s", dvdnav_err_to_string(d->dvdnav));
1160 /***********************************************************************
1161 * hb_dvdnav_set_angle
1162 ***********************************************************************
1163 * Sets the angle to read
1164 **********************************************************************/
1165 static void hb_dvdnav_set_angle( hb_dvd_t * e, int angle )
1167 hb_dvdnav_t * d = &(e->dvdnav);
1169 if (dvdnav_angle_change( d->dvdnav, angle) != DVDNAV_STATUS_OK)
1171 hb_log("dvdnav_angle_change %s", dvdnav_err_to_string(d->dvdnav));
1175 /***********************************************************************
1177 ***********************************************************************
1178 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1179 * cell to be read when we will be done with cell_cur.
1180 **********************************************************************/
1181 static int FindChapterIndex( hb_list_t * list, int pgcn, int pgn )
1184 hb_chapter_t *chapter;
1186 count = hb_list_count( list );
1187 for (ii = 0; ii < count; ii++)
1189 chapter = hb_list_item( list, ii );
1190 if (chapter->pgcn == pgcn && chapter->pgn == pgn)
1191 return chapter->index;
1196 /***********************************************************************
1198 ***********************************************************************
1199 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1200 * cell to be read when we will be done with cell_cur.
1201 **********************************************************************/
1202 static int FindNextCell( pgc_t *pgc, int cell_cur )
1207 if( pgc->cell_playback[cell_cur].block_type ==
1208 BLOCK_TYPE_ANGLE_BLOCK )
1211 while( pgc->cell_playback[cell_cur+i].block_mode !=
1212 BLOCK_MODE_LAST_CELL )
1216 cell_next = cell_cur + i + 1;
1217 hb_log( "dvd: Skipping multi-angle cells %d-%d",
1223 cell_next = cell_cur + 1;
1228 static uint8_t pgcn_map[1280];
1230 /***********************************************************************
1232 ***********************************************************************
1233 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1234 * cell to be read when we will be done with cell_cur.
1235 **********************************************************************/
1236 static int NextPgcn( ifo_handle_t *ifo, int pgcn )
1244 bit = 1 << (pgcn & 0x7);
1245 pgcn_map[byte] |= bit;
1247 pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
1248 next_pgcn = pgc->next_pgc_nr;
1249 if ( next_pgcn < 1 || next_pgcn > ifo->vts_pgcit->nr_of_pgci_srp )
1252 byte = next_pgcn >> 3;
1253 bit = 1 << (pgcn & 0x7);
1254 if (pgcn_map[byte] & bit)
1260 /***********************************************************************
1262 ***********************************************************************
1263 * Pgc links can loop. I track which have been visited in a bit vector
1264 * Initialize the bit vector to empty.
1265 **********************************************************************/
1266 static void PgcWalkInit( void )
1268 memset(pgcn_map, 0, 128);
1271 /***********************************************************************
1273 ***********************************************************************
1275 **********************************************************************/
1276 static int dvdtime2msec(dvd_time_t * dt)
1278 double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
1279 double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
1281 ms = (((dt->hour & 0xf0) >> 3) * 5 + (dt->hour & 0x0f)) * 3600000;
1282 ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
1283 ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
1287 ms += ((dt->frame_u & 0x30) >> 3) * 5 +
1288 (dt->frame_u & 0x0f) * 1000.0 / fps;