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 "dvdread/ifo_read.h"
12 #include "dvdread/ifo_print.h"
13 #include "dvdread/nav_read.h"
15 static hb_dvd_t * hb_dvdread_init( char * path );
16 static void hb_dvdread_close( hb_dvd_t ** _d );
17 static char * hb_dvdread_name( char * path );
18 static int hb_dvdread_title_count( hb_dvd_t * d );
19 static hb_title_t * hb_dvdread_title_scan( hb_dvd_t * d, int t );
20 static int hb_dvdread_start( hb_dvd_t * d, hb_title_t *title, int chapter );
21 static void hb_dvdread_stop( hb_dvd_t * d );
22 static int hb_dvdread_seek( hb_dvd_t * d, float f );
23 static int hb_dvdread_read( hb_dvd_t * d, hb_buffer_t * b );
24 static int hb_dvdread_chapter( hb_dvd_t * d );
25 static int hb_dvdread_angle_count( hb_dvd_t * d );
26 static void hb_dvdread_set_angle( hb_dvd_t * d, int angle );
28 hb_dvd_func_t hb_dvdread_func =
33 hb_dvdread_title_count,
34 hb_dvdread_title_scan,
40 hb_dvdread_angle_count,
44 static hb_dvd_func_t *dvd_methods = &hb_dvdread_func;
46 /***********************************************************************
48 **********************************************************************/
49 static void FindNextCell( hb_dvdread_t * );
50 static int dvdtime2msec( dvd_time_t * );
51 static int hb_dvdread_is_break( hb_dvdread_t * d );
53 hb_dvd_func_t * hb_dvdread_methods( void )
55 return &hb_dvdread_func;
58 static char * hb_dvdread_name( char * path )
60 static char name[1024];
61 unsigned char unused[1024];
62 dvd_reader_t * reader;
64 reader = DVDOpen( path );
70 if( DVDUDFVolumeInfo( reader, name, sizeof( name ),
71 unused, sizeof( unused ) ) )
81 /***********************************************************************
83 ***********************************************************************
85 **********************************************************************/
86 hb_dvd_t * hb_dvdread_init( char * path )
92 e = calloc( sizeof( hb_dvd_t ), 1 );
95 /* Log DVD drive region code */
96 if ( hb_dvd_region( path, ®ion_mask ) == 0 )
98 hb_log( "dvd: Region mask 0x%02x", region_mask );
99 if ( region_mask == 0xFF )
101 hb_log( "dvd: Warning, DVD device has no region set" );
106 if( !( d->reader = DVDOpen( path ) ) )
109 * Not an error, may be a stream - which we'll try in a moment.
111 hb_log( "dvd: not a dvd - trying as a stream/file instead" );
116 if( !( d->vmg = ifoOpen( d->reader, 0 ) ) )
118 hb_error( "dvd: ifoOpen failed" );
122 d->path = strdup( path );
127 if( d->vmg ) ifoClose( d->vmg );
128 if( d->reader ) DVDClose( d->reader );
133 /***********************************************************************
134 * hb_dvdread_title_count
135 **********************************************************************/
136 static int hb_dvdread_title_count( hb_dvd_t * e )
138 hb_dvdread_t *d = &(e->dvdread);
139 return d->vmg->tt_srpt->nr_of_srpts;
142 /***********************************************************************
143 * hb_dvdread_title_scan
144 **********************************************************************/
145 static hb_title_t * hb_dvdread_title_scan( hb_dvd_t * e, int t )
148 hb_dvdread_t *d = &(e->dvdread);
150 ifo_handle_t * vts = NULL;
152 hb_chapter_t * chapter;
155 float duration_correction;
156 unsigned char unused[1024];
158 hb_log( "scan: scanning title %d", t );
160 title = hb_title_init( d->path, t );
161 if( DVDUDFVolumeInfo( d->reader, title->name, sizeof( title->name ),
162 unused, sizeof( unused ) ) )
164 char * p_cur, * p_last = d->path;
165 for( p_cur = d->path; *p_cur; p_cur++ )
167 if( p_cur[0] == '/' && p_cur[1] )
172 snprintf( title->name, sizeof( title->name ), "%s", p_last );
175 /* VTS which our title is in */
176 title->vts = d->vmg->tt_srpt->title[t-1].title_set_nr;
180 /* A VTS of 0 means the title wasn't found in the title set */
181 hb_error("Invalid VTS (title set) number: %i", title->vts);
185 hb_log( "scan: opening IFO for VTS %d", title->vts );
186 if( !( vts = ifoOpen( d->reader, title->vts ) ) )
188 hb_error( "scan: ifoOpen failed" );
192 /* ignore titles with bogus cell addresses so we don't abort later
194 for ( i = 0; i < vts->vts_c_adt->nr_of_vobs; ++i)
196 if( (vts->vts_c_adt->cell_adr_table[i].start_sector & 0xffffff ) ==
199 hb_error( "scan: cell_adr_table[%d].start_sector invalid (0x%x) "
200 "- skipping title", i,
201 vts->vts_c_adt->cell_adr_table[i].start_sector );
204 if( (vts->vts_c_adt->cell_adr_table[i].last_sector & 0xffffff ) ==
207 hb_error( "scan: cell_adr_table[%d].last_sector invalid (0x%x) "
208 "- skipping title", i,
209 vts->vts_c_adt->cell_adr_table[i].last_sector );
212 if( vts->vts_c_adt->cell_adr_table[i].start_sector >=
213 vts->vts_c_adt->cell_adr_table[i].last_sector )
215 hb_error( "scan: cell_adr_table[%d].start_sector (0x%x) "
216 "is not before last_sector (0x%x) - skipping title", i,
217 vts->vts_c_adt->cell_adr_table[i].start_sector,
218 vts->vts_c_adt->cell_adr_table[i].last_sector );
223 if( global_verbosity_level == 3 )
225 ifo_print( d->reader, title->vts );
228 /* Position of the title in the VTS */
229 title->ttn = d->vmg->tt_srpt->title[t-1].vts_ttn;
230 if ( title->ttn < 1 || title->ttn > vts->vts_ptt_srpt->nr_of_srpts )
232 hb_error( "invalid VTS PTT offset %d for title %d, skipping", title->ttn, t );
237 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
238 if ( pgc_id < 1 || pgc_id > vts->vts_pgcit->nr_of_pgci_srp )
240 hb_error( "invalid PGC ID %d for title %d, skipping", pgc_id, t );
243 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
244 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
246 hb_log("pgc_id: %d, pgn: %d: pgc: %p", pgc_id, pgn, d->pgc);
250 hb_error( "scan: pgc not valid, skipping" );
254 if( pgn <= 0 || pgn > 99 )
256 hb_error( "scan: pgn %d not valid, skipping", pgn );
261 title->cell_start = d->pgc->program_map[pgn-1] - 1;
262 title->block_start = d->pgc->cell_playback[title->cell_start].first_sector;
265 title->cell_end = d->pgc->nr_of_cells - 1;
266 title->block_end = d->pgc->cell_playback[title->cell_end].last_sector;
269 title->block_count = 0;
270 d->cell_cur = title->cell_start;
271 while( d->cell_cur <= title->cell_end )
273 #define cp d->pgc->cell_playback[d->cell_cur]
274 title->block_count += cp.last_sector + 1 - cp.first_sector;
277 d->cell_cur = d->cell_next;
280 hb_log( "scan: vts=%d, ttn=%d, cells=%d->%d, blocks=%d->%d, "
281 "%d blocks", title->vts, title->ttn, title->cell_start,
282 title->cell_end, title->block_start, title->block_end,
283 title->block_count );
286 title->duration = 90LL * dvdtime2msec( &d->pgc->playback_time );
287 title->hours = title->duration / 90000 / 3600;
288 title->minutes = ( ( title->duration / 90000 ) % 3600 ) / 60;
289 title->seconds = ( title->duration / 90000 ) % 60;
290 hb_log( "scan: duration is %02d:%02d:%02d (%"PRId64" ms)",
291 title->hours, title->minutes, title->seconds,
292 title->duration / 90 );
294 /* ignore titles under 10 seconds because they're often stills or
295 * clips with no audio & our preview code doesn't currently handle
296 * either of these. */
297 if( title->duration < 900000LL )
299 hb_log( "scan: ignoring title (too short)" );
303 /* Detect languages */
304 for( i = 0; i < vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
306 hb_audio_t * audio, * audio_tmp;
307 int audio_format, lang_code, audio_control,
309 iso639_lang_t * lang;
310 int lang_extension = 0;
312 hb_log( "scan: checking audio %d", i + 1 );
314 audio = calloc( sizeof( hb_audio_t ), 1 );
316 audio_format = vts->vtsi_mat->vts_audio_attr[i].audio_format;
317 lang_code = vts->vtsi_mat->vts_audio_attr[i].lang_code;
318 lang_extension = vts->vtsi_mat->vts_audio_attr[i].code_extension;
320 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i];
322 if( !( audio_control & 0x8000 ) )
324 hb_log( "scan: audio channel is not active" );
329 position = ( audio_control & 0x7F00 ) >> 8;
331 switch( audio_format )
334 audio->id = ( ( 0x80 + position ) << 8 ) | 0xbd;
335 audio->config.in.codec = HB_ACODEC_AC3;
340 audio->id = 0xc0 + position;
341 audio->config.in.codec = HB_ACODEC_MPGA;
345 audio->id = ( ( 0xa0 + position ) << 8 ) | 0xbd;
346 audio->config.in.codec = HB_ACODEC_LPCM;
350 audio->id = ( ( 0x88 + position ) << 8 ) | 0xbd;
351 audio->config.in.codec = HB_ACODEC_DCA;
356 audio->config.in.codec = 0;
357 hb_log( "scan: unknown audio codec (%x)",
366 /* Check for duplicate tracks */
368 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
370 audio_tmp = hb_list_item( title->list_audio, j );
371 if( audio->id == audio_tmp->id )
379 hb_log( "scan: duplicate audio track" );
384 audio->config.lang.type = lang_extension;
386 lang = lang_for_code( vts->vtsi_mat->vts_audio_attr[i].lang_code );
388 snprintf( audio->config.lang.description, sizeof( audio->config.lang.description ), "%s (%s)",
389 strlen(lang->native_name) ? lang->native_name : lang->eng_name,
390 audio->config.in.codec == HB_ACODEC_AC3 ? "AC3" : ( audio->config.in.codec ==
391 HB_ACODEC_DCA ? "DTS" : ( audio->config.in.codec ==
392 HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) ) );
393 snprintf( audio->config.lang.simple, sizeof( audio->config.lang.simple ), "%s",
394 strlen(lang->native_name) ? lang->native_name : lang->eng_name );
395 snprintf( audio->config.lang.iso639_2, sizeof( audio->config.lang.iso639_2 ), "%s",
398 switch( lang_extension )
404 strcat( audio->config.lang.description, " (Visually Impaired)" );
407 strcat( audio->config.lang.description, " (Director's Commentary 1)" );
410 strcat( audio->config.lang.description, " (Director's Commentary 2)" );
416 hb_log( "scan: id=%x, lang=%s, 3cc=%s ext=%i", audio->id,
417 audio->config.lang.description, audio->config.lang.iso639_2,
420 audio->config.in.track = i;
421 hb_list_add( title->list_audio, audio );
424 if( !hb_list_count( title->list_audio ) )
426 hb_log( "scan: ignoring title (no audio track)" );
430 memcpy( title->palette,
431 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette,
432 16 * sizeof( uint32_t ) );
434 /* Check for subtitles */
435 for( i = 0; i < vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
437 hb_subtitle_t * subtitle;
440 iso639_lang_t * lang;
441 int lang_extension = 0;
443 hb_log( "scan: checking subtitle %d", i + 1 );
446 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i];
448 if( !( spu_control & 0x80000000 ) )
450 hb_log( "scan: subtitle channel is not active" );
454 if( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
456 switch( vts->vtsi_mat->vts_video_attr.permitted_df )
459 position = spu_control & 0xFF;
462 position = ( spu_control >> 8 ) & 0xFF;
465 position = ( spu_control >> 16 ) & 0xFF;
470 position = ( spu_control >> 24 ) & 0x7F;
473 lang_extension = vts->vtsi_mat->vts_subp_attr[i].code_extension;
475 lang = lang_for_code( vts->vtsi_mat->vts_subp_attr[i].lang_code );
477 subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
478 subtitle->track = i+1;
479 subtitle->id = ( ( 0x20 + position ) << 8 ) | 0xbd;
480 snprintf( subtitle->lang, sizeof( subtitle->lang ), "%s",
481 strlen(lang->native_name) ? lang->native_name : lang->eng_name);
482 snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ), "%s",
484 subtitle->format = PICTURESUB;
485 subtitle->source = VOBSUB;
486 subtitle->config.dest = RENDERSUB; // By default render (burn-in) the VOBSUB.
488 subtitle->type = lang_extension;
490 switch( lang_extension )
497 strcat( subtitle->lang, " (Caption with bigger size character)");
500 strcat( subtitle->lang, " (Caption for Children)");
505 strcat( subtitle->lang, " (Closed Caption)");
508 strcat( subtitle->lang, " (Closed Caption with bigger size character)");
511 strcat( subtitle->lang, " (Closed Caption for Children)");
516 strcat( subtitle->lang, " (Forced Caption)");
525 strcat( subtitle->lang, " (Director's Commentary)");
528 strcat( subtitle->lang, " (Director's Commentary with bigger size character)");
531 strcat( subtitle->lang, " (Director's Commentary for Children)");
536 hb_log( "scan: id=%x, lang=%s, 3cc=%s", subtitle->id,
537 subtitle->lang, subtitle->iso639_2 );
539 hb_list_add( title->list_subtitle, subtitle );
543 hb_log( "scan: title %d has %d chapters", t,
544 vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts );
546 i < vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts; i++ )
548 chapter = calloc( sizeof( hb_chapter_t ), 1 );
549 /* remember the on-disc chapter number */
550 chapter->index = i + 1;
552 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgcn;
553 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgn;
554 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
557 chapter->cell_start = d->pgc->program_map[pgn-1] - 1;
558 chapter->block_start =
559 d->pgc->cell_playback[chapter->cell_start].first_sector;
561 // if there are no more programs in this pgc, the end cell is the
562 // last cell. Otherwise it's the cell before the start cell of the
564 if ( pgn == d->pgc->nr_of_programs )
566 chapter->cell_end = d->pgc->nr_of_cells - 1;
570 chapter->cell_end = d->pgc->program_map[pgn] - 2;;
572 chapter->block_end = d->pgc->cell_playback[chapter->cell_end].last_sector;
574 /* Block count, duration */
575 chapter->block_count = 0;
576 chapter->duration = 0;
578 d->cell_cur = chapter->cell_start;
579 while( d->cell_cur <= chapter->cell_end )
581 #define cp d->pgc->cell_playback[d->cell_cur]
582 chapter->block_count += cp.last_sector + 1 - cp.first_sector;
583 chapter->duration += 90LL * dvdtime2msec( &cp.playback_time );
586 d->cell_cur = d->cell_next;
588 hb_list_add( title->list_chapter, chapter );
592 /* The durations we get for chapters aren't precise. Scale them so
593 the total matches the title duration */
595 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
597 chapter = hb_list_item( title->list_chapter, i );
598 duration += chapter->duration;
600 duration_correction = (float) title->duration / (float) duration;
601 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
604 chapter = hb_list_item( title->list_chapter, i );
605 chapter->duration = duration_correction * chapter->duration;
606 seconds = ( chapter->duration + 45000 ) / 90000;
607 chapter->hours = seconds / 3600;
608 chapter->minutes = ( seconds % 3600 ) / 60;
609 chapter->seconds = seconds % 60;
611 hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %"PRId64" ms",
612 chapter->index, chapter->cell_start, chapter->cell_end,
613 chapter->block_start, chapter->block_end,
614 chapter->block_count, chapter->duration / 90 );
617 /* Get aspect. We don't get width/height/rate infos here as
618 they tend to be wrong */
619 switch( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
622 title->container_aspect = 4. / 3.;
625 title->container_aspect = 16. / 9.;
628 hb_log( "scan: unknown aspect" );
632 hb_log( "scan: aspect = %g", title->aspect );
634 /* This title is ok so far */
638 hb_list_close( &title->list_audio );
643 if( vts ) ifoClose( vts );
648 /***********************************************************************
650 ***********************************************************************
651 * Title and chapter start at 1
652 **********************************************************************/
653 static int hb_dvdread_start( hb_dvd_t * e, hb_title_t *title, int chapter )
655 hb_dvdread_t *d = &(e->dvdread);
658 int t = title->index;
660 /* Open the IFO and the VOBs for this title */
661 d->vts = d->vmg->tt_srpt->title[t-1].title_set_nr;
662 d->ttn = d->vmg->tt_srpt->title[t-1].vts_ttn;
663 if( !( d->ifo = ifoOpen( d->reader, d->vts ) ) )
665 hb_error( "dvd: ifoOpen failed for VTS %d", d->vts );
668 if( !( d->file = DVDOpenFile( d->reader, d->vts,
669 DVD_READ_TITLE_VOBS ) ) )
671 hb_error( "dvd: DVDOpenFile failed for VTS %d", d->vts );
675 /* Get title first and last blocks */
676 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgcn;
677 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgn;
678 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
679 d->cell_start = d->pgc->program_map[pgn - 1] - 1;
680 d->cell_end = d->pgc->nr_of_cells - 1;
681 d->title_start = d->pgc->cell_playback[d->cell_start].first_sector;
682 d->title_end = d->pgc->cell_playback[d->cell_end].last_sector;
685 d->title_block_count = 0;
686 for( i = d->cell_start; i <= d->cell_end; i++ )
688 d->title_block_count += d->pgc->cell_playback[i].last_sector + 1 -
689 d->pgc->cell_playback[i].first_sector;
692 /* Get pgc for the current chapter */
693 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgcn;
694 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgn;
695 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
697 /* Get the two first cells */
698 d->cell_cur = d->pgc->program_map[pgn-1] - 1;
701 d->block = d->pgc->cell_playback[d->cell_cur].first_sector;
702 d->next_vobu = d->block;
711 /***********************************************************************
713 ***********************************************************************
715 **********************************************************************/
716 static void hb_dvdread_stop( hb_dvd_t * e )
718 hb_dvdread_t *d = &(e->dvdread);
726 DVDCloseFile( d->file );
731 /***********************************************************************
733 ***********************************************************************
735 **********************************************************************/
736 static int hb_dvdread_seek( hb_dvd_t * e, float f )
738 hb_dvdread_t *d = &(e->dvdread);
742 count = f * d->title_block_count;
744 for( i = d->cell_start; i <= d->cell_end; i++ )
746 sizeCell = d->pgc->cell_playback[i].last_sector + 1 -
747 d->pgc->cell_playback[i].first_sector;
749 if( count < sizeCell )
755 /* Now let hb_dvdread_read find the next VOBU */
756 d->next_vobu = d->pgc->cell_playback[i].first_sector + count;
764 if( i > d->cell_end )
770 * Assume that we are in sync, even if we are not given that it is obvious
771 * that we are out of sync if we have just done a seek.
779 /***********************************************************************
781 ***********************************************************************/
782 int is_nav_pack( unsigned char *buf )
785 * The NAV Pack is comprised of the PCI Packet and DSI Packet, both
786 * of these start at known offsets and start with a special identifier.
789 * PCI = { 00 00 01 bf # private stream header
794 * DSI = { 00 00 01 bf # private stream header
800 * The PCI starts at offset 0x26 into the sector, and the DSI starts at 0x400
802 * This information from: http://dvd.sourceforge.net/dvdinfo/
804 if( ( buf[0x26] == 0x00 && // PCI
808 buf[0x2c] == 0x00 ) &&
809 ( buf[0x400] == 0x00 && // DSI
810 buf[0x401] == 0x00 &&
811 buf[0x402] == 0x01 &&
812 buf[0x403] == 0xbf &&
813 buf[0x406] == 0x01 ) )
822 /***********************************************************************
824 ***********************************************************************
826 **********************************************************************/
827 static int hb_dvdread_read( hb_dvd_t * e, hb_buffer_t * b )
829 hb_dvdread_t *d = &(e->dvdread);
837 // if we've just finished the last cell of the title we don't
838 // want to read another block because our next_vobu pointer
839 // is probably invalid. Just return 'no data' & our caller
840 // should check and discover we're at eof.
841 if ( d->cell_cur > d->cell_end )
846 int block, pack_len, next_vobu, read_retry;
848 for( read_retry = 0; read_retry < 3; read_retry++ )
850 if( DVDReadBlocks( d->file, d->next_vobu, 1, b->data ) == 1 )
857 hb_log( "dvd: Read Error on blk %d, attempt %d",
858 d->next_vobu, read_retry );
862 if( read_retry == 3 )
864 hb_log( "dvd: vobu read error blk %d - skipping to cell %d",
865 d->next_vobu, d->cell_next );
866 d->cell_cur = d->cell_next;
867 if ( d->cell_cur > d->cell_end )
870 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
876 if ( !is_nav_pack( b->data ) ) {
878 if( d->in_sync == 1 ) {
879 hb_log("dvd: Lost sync, searching for NAV pack at blk %d",
886 navRead_DSI( &dsi_pack, &b->data[DSI_START_BYTE] );
888 if ( d->in_sync == 0 && d->cur_cell_id &&
889 (d->cur_vob_id != dsi_pack.dsi_gi.vobu_vob_idn ||
890 d->cur_cell_id != dsi_pack.dsi_gi.vobu_c_idn ) )
892 // We walked out of the cell we're supposed to be in.
893 // If we're not at the start of our next cell go there.
894 hb_log("dvd: left cell %d (%u,%u) for (%u,%u) at block %u",
895 d->cell_cur, d->cur_vob_id, d->cur_cell_id,
896 dsi_pack.dsi_gi.vobu_vob_idn, dsi_pack.dsi_gi.vobu_c_idn,
898 if ( d->next_vobu != d->pgc->cell_playback[d->cell_next].first_sector )
900 d->next_vobu = d->pgc->cell_playback[d->cell_next].first_sector;
906 block = dsi_pack.dsi_gi.nv_pck_lbn;
907 pack_len = dsi_pack.dsi_gi.vobu_ea;
909 // There are a total of 21 next vobu offsets (and 21 prev_vobu
910 // offsets) in the navpack SRI structure. The primary one is
911 // 'next_vobu' which is the offset (in dvd blocks) from the current
912 // block to the start of the next vobu. If the block at 'next_vobu'
913 // can't be read, 'next_video' is the offset to the vobu closest to it.
914 // The other 19 offsets are vobus at successively longer distances from
915 // the current block (this is so that a hardware player can do
916 // adaptive error recovery to skip over a bad spot on the disk). In all
917 // these offsets the high bit is set to indicate when it contains a
918 // valid offset. The next bit (2^30) is set to indicate that there's
919 // another valid offset in the SRI that's closer to the current block.
920 // A hardware player uses these bits to chose the closest valid offset
921 // and uses that as its next vobu. (Some mastering schemes appear to
922 // put a bogus offset in next_vobu with the 2^30 bit set & the
923 // correct offset in next_video. This works fine in hardware players
924 // but will mess up software that doesn't implement the full next
925 // vobu decision logic.) In addition to the flag bits there's a
926 // reserved value of the offset that indicates 'no next vobu' (which
927 // indicates the end of a cell). But having no next vobu pointer with a
928 // 'valid' bit set also indicates end of cell. Different mastering
929 // schemes seem to use all possible combinations of the flag bits
930 // and reserved values to indicate end of cell so we have to check
931 // them all or we'll get a disk read error from following an
933 uint32_t next_ptr = dsi_pack.vobu_sri.next_vobu;
934 if ( ( next_ptr & ( 1 << 31 ) ) == 0 ||
935 ( next_ptr & ( 1 << 30 ) ) != 0 ||
936 ( next_ptr & 0x3fffffff ) == 0x3fffffff )
938 next_ptr = dsi_pack.vobu_sri.next_video;
939 if ( ( next_ptr & ( 1 << 31 ) ) == 0 ||
940 ( next_ptr & 0x3fffffff ) == 0x3fffffff )
942 // don't have a valid forward pointer - assume end-of-cell
944 d->pack_len = pack_len;
948 next_vobu = block + ( next_ptr & 0x3fffffff );
952 block >= d->next_vobu &&
953 ( block <= d->title_start + d->title_block_count ||
954 block <= d->title_end ) )
957 d->pack_len = pack_len;
958 d->next_vobu = next_vobu;
962 /* Wasn't a valid VOBU, try next block */
965 hb_log( "dvd: couldn't find a VOBU after 1024 blocks" );
972 if( d->in_sync == 0 || d->in_sync == 2 )
974 if( d->in_sync == 0 )
976 hb_log( "dvd: In sync with DVD at block %d", d->block );
981 // Revert the cell overlap, and check for a chapter break
982 // If this cell is zero length (prev_vobu & next_vobu both
983 // set to END_OF_CELL) we need to check for beginning of
984 // cell before checking for end or we'll advance to the next
985 // cell too early and fail to generate a chapter mark when this
986 // cell starts a chapter.
987 if( ( dsi_pack.vobu_sri.prev_vobu & (1 << 31 ) ) == 0 ||
988 ( dsi_pack.vobu_sri.prev_vobu & 0x3fffffff ) == 0x3fffffff )
990 // A vobu that's not at the start of a cell can have an
991 // EOC prev pointer (this seems to be associated with some
992 // sort of drm). The rest of the content in the cell may be
993 // booby-trapped so treat this like an end-of-cell rather
994 // than a beginning of cell.
995 if ( d->pgc->cell_playback[d->cell_cur].first_sector < dsi_pack.dsi_gi.nv_pck_lbn &&
996 d->pgc->cell_playback[d->cell_cur].last_sector >= dsi_pack.dsi_gi.nv_pck_lbn )
998 hb_log( "dvd: null prev_vobu in cell %d at block %d", d->cell_cur,
1000 // treat like end-of-cell then go directly to start of next cell.
1001 d->cell_cur = d->cell_next;
1003 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
1005 d->cell_overlap = 1;
1010 if ( d->block != d->pgc->cell_playback[d->cell_cur].first_sector )
1012 hb_log( "dvd: beginning of cell %d at block %d", d->cell_cur,
1017 hb_error( "dvd: assuming missed end of cell %d", d->cell_cur );
1018 d->cell_cur = d->cell_next;
1019 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
1021 d->cell_overlap = 1;
1026 d->cur_vob_id = dsi_pack.dsi_gi.vobu_vob_idn;
1027 d->cur_cell_id = dsi_pack.dsi_gi.vobu_c_idn;
1029 if( d->cell_overlap )
1031 b->new_chap = hb_dvdread_is_break( d );
1032 d->cell_overlap = 0;
1037 if( ( dsi_pack.vobu_sri.next_vobu & (1 << 31 ) ) == 0 ||
1038 ( dsi_pack.vobu_sri.next_vobu & 0x3fffffff ) == 0x3fffffff )
1040 if ( d->block <= d->pgc->cell_playback[d->cell_cur].first_sector ||
1041 d->block > d->pgc->cell_playback[d->cell_cur].last_sector )
1043 hb_log( "dvd: end of cell %d at block %d", d->cell_cur,
1046 d->cell_cur = d->cell_next;
1048 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
1050 d->cell_overlap = 1;
1056 if( DVDReadBlocks( d->file, d->block, 1, b->data ) != 1 )
1058 // this may be a real DVD error or may be DRM. Either way
1059 // we don't want to quit because of one bad block so set
1060 // things up so we'll advance to the next vobu and recurse.
1061 hb_error( "dvd: DVDReadBlocks failed (%d), skipping to vobu %u",
1062 d->block, d->next_vobu );
1064 goto top; /* XXX need to restructure this routine & avoid goto */
1074 /***********************************************************************
1075 * hb_dvdread_chapter
1076 ***********************************************************************
1077 * Returns in which chapter the next block to be read is.
1078 * Chapter numbers start at 1.
1079 **********************************************************************/
1080 static int hb_dvdread_chapter( hb_dvd_t * e )
1082 hb_dvdread_t *d = &(e->dvdread);
1085 int nr_of_ptts = d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
1088 for( i = nr_of_ptts - 1;
1092 /* Get pgc for chapter (i+1) */
1093 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
1094 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
1095 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1097 if( d->cell_cur - d->cell_overlap >= pgc->program_map[pgn-1] - 1 &&
1098 d->cell_cur - d->cell_overlap <= pgc->nr_of_cells - 1 )
1100 /* We are in this chapter */
1109 /***********************************************************************
1110 * hb_dvdread_is_break
1111 ***********************************************************************
1112 * Returns chapter number if the current block is a new chapter start
1113 **********************************************************************/
1114 static int hb_dvdread_is_break( hb_dvdread_t * d )
1118 int nr_of_ptts = d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
1122 for( i = nr_of_ptts - 1;
1126 /* Get pgc for chapter (i+1) */
1127 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
1128 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
1129 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1130 cell = pgc->program_map[pgn-1] - 1;
1132 if( cell <= d->cell_start )
1135 // This must not match against the start cell.
1136 if( pgc->cell_playback[cell].first_sector == d->block && cell != d->cell_start )
1145 /***********************************************************************
1147 ***********************************************************************
1148 * Closes and frees everything
1149 **********************************************************************/
1150 static void hb_dvdread_close( hb_dvd_t ** _d )
1152 hb_dvdread_t * d = &((*_d)->dvdread);
1160 DVDClose( d->reader );
1167 /***********************************************************************
1168 * hb_dvdread_angle_count
1169 ***********************************************************************
1170 * Returns the number of angles supported. We do not support angles
1172 **********************************************************************/
1173 static int hb_dvdread_angle_count( hb_dvd_t * d )
1178 /***********************************************************************
1179 * hb_dvdread_set_angle
1180 ***********************************************************************
1181 * Sets the angle to read. Not supported with dvdread
1182 **********************************************************************/
1183 static void hb_dvdread_set_angle( hb_dvd_t * d, int angle )
1187 /***********************************************************************
1189 ***********************************************************************
1190 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1191 * cell to be read when we will be done with cell_cur.
1192 **********************************************************************/
1193 static void FindNextCell( hb_dvdread_t * d )
1197 if( d->pgc->cell_playback[d->cell_cur].block_type ==
1198 BLOCK_TYPE_ANGLE_BLOCK )
1201 while( d->pgc->cell_playback[d->cell_cur+i].block_mode !=
1202 BLOCK_MODE_LAST_CELL )
1206 d->cell_next = d->cell_cur + i + 1;
1207 hb_log( "dvd: Skipping multi-angle cells %d-%d",
1213 d->cell_next = d->cell_cur + 1;
1217 /***********************************************************************
1219 ***********************************************************************
1221 **********************************************************************/
1222 static int dvdtime2msec(dvd_time_t * dt)
1224 double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
1225 double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
1227 ms = (((dt->hour & 0xf0) >> 3) * 5 + (dt->hour & 0x0f)) * 3600000;
1228 ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
1229 ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
1233 ms += ((dt->frame_u & 0x30) >> 3) * 5 +
1234 (dt->frame_u & 0x0f) * 1000.0 / fps;
1240 char * hb_dvd_name( char * path )
1242 return dvd_methods->name(path);
1245 hb_dvd_t * hb_dvd_init( char * path )
1247 return dvd_methods->init(path);
1250 int hb_dvd_title_count( hb_dvd_t * d )
1252 return dvd_methods->title_count(d);
1255 hb_title_t * hb_dvd_title_scan( hb_dvd_t * d, int t )
1257 return dvd_methods->title_scan(d, t);
1260 int hb_dvd_start( hb_dvd_t * d, hb_title_t *title, int chapter )
1262 return dvd_methods->start(d, title, chapter);
1265 void hb_dvd_stop( hb_dvd_t * d )
1267 dvd_methods->stop(d);
1270 int hb_dvd_seek( hb_dvd_t * d, float f )
1272 return dvd_methods->seek(d, f);
1275 int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
1277 return dvd_methods->read(d, b);
1280 int hb_dvd_chapter( hb_dvd_t * d )
1282 return dvd_methods->chapter(d);
1285 void hb_dvd_close( hb_dvd_t ** _d )
1287 dvd_methods->close(_d);
1290 int hb_dvd_angle_count( hb_dvd_t * d )
1292 return dvd_methods->angle_count(d);
1295 void hb_dvd_set_angle( hb_dvd_t * d, int angle )
1297 dvd_methods->set_angle(d, angle);
1300 // hb_dvd_set_dvdnav must only be called when no dvd source is open
1301 // it rips the rug out from under things so be careful
1302 void hb_dvd_set_dvdnav( int enable )
1305 dvd_methods = hb_dvdnav_methods();
1307 dvd_methods = hb_dvdread_methods();