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/nav_read.h"
14 static hb_dvd_t * hb_dvdread_init( char * path );
15 static void hb_dvdread_close( hb_dvd_t ** _d );
16 static char * hb_dvdread_name( char * path );
17 static int hb_dvdread_title_count( hb_dvd_t * d );
18 static hb_title_t * hb_dvdread_title_scan( hb_dvd_t * d, int t );
19 static int hb_dvdread_start( hb_dvd_t * d, hb_title_t *title, int chapter );
20 static void hb_dvdread_stop( hb_dvd_t * d );
21 static int hb_dvdread_seek( hb_dvd_t * d, float f );
22 static int hb_dvdread_read( hb_dvd_t * d, hb_buffer_t * b );
23 static int hb_dvdread_chapter( hb_dvd_t * d );
24 static int hb_dvdread_angle_count( hb_dvd_t * d );
25 static void hb_dvdread_set_angle( hb_dvd_t * d, int angle );
27 hb_dvd_func_t hb_dvdread_func =
32 hb_dvdread_title_count,
33 hb_dvdread_title_scan,
39 hb_dvdread_angle_count,
43 static hb_dvd_func_t *dvd_methods = &hb_dvdread_func;
45 /***********************************************************************
47 **********************************************************************/
48 static void FindNextCell( hb_dvdread_t * );
49 static int dvdtime2msec( dvd_time_t * );
50 static int hb_dvdread_is_break( hb_dvdread_t * d );
52 hb_dvd_func_t * hb_dvdread_methods( void )
54 return &hb_dvdread_func;
57 static char * hb_dvdread_name( char * path )
59 static char name[1024];
60 unsigned char unused[1024];
61 dvd_reader_t * reader;
63 reader = DVDOpen( path );
69 if( DVDUDFVolumeInfo( reader, name, sizeof( name ),
70 unused, sizeof( unused ) ) )
80 /***********************************************************************
82 ***********************************************************************
84 **********************************************************************/
85 hb_dvd_t * hb_dvdread_init( char * path )
90 e = calloc( sizeof( hb_dvd_t ), 1 );
94 if( !( d->reader = DVDOpen( path ) ) )
97 * Not an error, may be a stream - which we'll try in a moment.
99 hb_log( "dvd: not a dvd - trying as a stream/file instead" );
104 if( !( d->vmg = ifoOpen( d->reader, 0 ) ) )
106 hb_error( "dvd: ifoOpen failed" );
110 d->path = strdup( path );
115 if( d->vmg ) ifoClose( d->vmg );
116 if( d->reader ) DVDClose( d->reader );
121 /***********************************************************************
122 * hb_dvdread_title_count
123 **********************************************************************/
124 static int hb_dvdread_title_count( hb_dvd_t * e )
126 hb_dvdread_t *d = &(e->dvdread);
127 return d->vmg->tt_srpt->nr_of_srpts;
130 /***********************************************************************
131 * hb_dvdread_title_scan
132 **********************************************************************/
133 static hb_title_t * hb_dvdread_title_scan( hb_dvd_t * e, int t )
136 hb_dvdread_t *d = &(e->dvdread);
138 ifo_handle_t * vts = NULL;
140 hb_chapter_t * chapter;
143 float duration_correction;
144 unsigned char unused[1024];
146 hb_log( "scan: scanning title %d", t );
148 title = hb_title_init( d->path, t );
149 if( DVDUDFVolumeInfo( d->reader, title->name, sizeof( title->name ),
150 unused, sizeof( unused ) ) )
152 char * p_cur, * p_last = d->path;
153 for( p_cur = d->path; *p_cur; p_cur++ )
155 if( p_cur[0] == '/' && p_cur[1] )
160 snprintf( title->name, sizeof( title->name ), "%s", p_last );
163 /* VTS which our title is in */
164 title->vts = d->vmg->tt_srpt->title[t-1].title_set_nr;
168 /* A VTS of 0 means the title wasn't found in the title set */
169 hb_error("Invalid VTS (title set) number: %i", title->vts);
173 hb_log( "scan: opening IFO for VTS %d", title->vts );
174 if( !( vts = ifoOpen( d->reader, title->vts ) ) )
176 hb_error( "scan: ifoOpen failed" );
180 /* ignore titles with bogus cell addresses so we don't abort later
182 for ( i = 0; i < vts->vts_c_adt->nr_of_vobs; ++i)
184 if( (vts->vts_c_adt->cell_adr_table[i].start_sector & 0xffffff ) ==
187 hb_error( "scan: cell_adr_table[%d].start_sector invalid (0x%x) "
188 "- skipping title", i,
189 vts->vts_c_adt->cell_adr_table[i].start_sector );
192 if( (vts->vts_c_adt->cell_adr_table[i].last_sector & 0xffffff ) ==
195 hb_error( "scan: cell_adr_table[%d].last_sector invalid (0x%x) "
196 "- skipping title", i,
197 vts->vts_c_adt->cell_adr_table[i].last_sector );
200 if( vts->vts_c_adt->cell_adr_table[i].start_sector >=
201 vts->vts_c_adt->cell_adr_table[i].last_sector )
203 hb_error( "scan: cell_adr_table[%d].start_sector (0x%x) "
204 "is not before last_sector (0x%x) - skipping title", i,
205 vts->vts_c_adt->cell_adr_table[i].start_sector,
206 vts->vts_c_adt->cell_adr_table[i].last_sector );
211 if( global_verbosity_level == 3 )
213 ifo_print( d->reader, title->vts );
216 /* Position of the title in the VTS */
217 title->ttn = d->vmg->tt_srpt->title[t-1].vts_ttn;
218 if ( title->ttn < 1 || title->ttn > vts->vts_ptt_srpt->nr_of_srpts )
220 hb_error( "invalid VTS PTT offset %d for title %d, skipping", title->ttn, t );
225 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
226 if ( pgc_id < 1 || pgc_id > vts->vts_pgcit->nr_of_pgci_srp )
228 hb_error( "invalid PGC ID %d for title %d, skipping", pgc_id, t );
231 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
232 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
234 hb_log("pgc_id: %d, pgn: %d: pgc: 0x%x", pgc_id, pgn, d->pgc);
238 hb_error( "scan: pgc not valid, skipping" );
242 if( pgn <= 0 || pgn > 99 )
244 hb_error( "scan: pgn %d not valid, skipping", pgn );
249 title->cell_start = d->pgc->program_map[pgn-1] - 1;
250 title->block_start = d->pgc->cell_playback[title->cell_start].first_sector;
253 title->cell_end = d->pgc->nr_of_cells - 1;
254 title->block_end = d->pgc->cell_playback[title->cell_end].last_sector;
257 title->block_count = 0;
258 d->cell_cur = title->cell_start;
259 while( d->cell_cur <= title->cell_end )
261 #define cp d->pgc->cell_playback[d->cell_cur]
262 title->block_count += cp.last_sector + 1 - cp.first_sector;
265 d->cell_cur = d->cell_next;
268 hb_log( "scan: vts=%d, ttn=%d, cells=%d->%d, blocks=%d->%d, "
269 "%d blocks", title->vts, title->ttn, title->cell_start,
270 title->cell_end, title->block_start, title->block_end,
271 title->block_count );
274 title->duration = 90LL * dvdtime2msec( &d->pgc->playback_time );
275 title->hours = title->duration / 90000 / 3600;
276 title->minutes = ( ( title->duration / 90000 ) % 3600 ) / 60;
277 title->seconds = ( title->duration / 90000 ) % 60;
278 hb_log( "scan: duration is %02d:%02d:%02d (%lld ms)",
279 title->hours, title->minutes, title->seconds,
280 title->duration / 90 );
282 /* ignore titles under 10 seconds because they're often stills or
283 * clips with no audio & our preview code doesn't currently handle
284 * either of these. */
285 if( title->duration < 900000LL )
287 hb_log( "scan: ignoring title (too short)" );
291 /* Detect languages */
292 for( i = 0; i < vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
294 hb_audio_t * audio, * audio_tmp;
295 int audio_format, lang_code, audio_control,
297 iso639_lang_t * lang;
298 int lang_extension = 0;
300 hb_log( "scan: checking audio %d", i + 1 );
302 audio = calloc( sizeof( hb_audio_t ), 1 );
304 audio_format = vts->vtsi_mat->vts_audio_attr[i].audio_format;
305 lang_code = vts->vtsi_mat->vts_audio_attr[i].lang_code;
306 lang_extension = vts->vtsi_mat->vts_audio_attr[i].code_extension;
308 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i];
310 if( !( audio_control & 0x8000 ) )
312 hb_log( "scan: audio channel is not active" );
317 position = ( audio_control & 0x7F00 ) >> 8;
319 switch( audio_format )
322 audio->id = ( ( 0x80 + position ) << 8 ) | 0xbd;
323 audio->config.in.codec = HB_ACODEC_AC3;
328 audio->id = 0xc0 + position;
329 audio->config.in.codec = HB_ACODEC_MPGA;
333 audio->id = ( ( 0xa0 + position ) << 8 ) | 0xbd;
334 audio->config.in.codec = HB_ACODEC_LPCM;
338 audio->id = ( ( 0x88 + position ) << 8 ) | 0xbd;
339 audio->config.in.codec = HB_ACODEC_DCA;
344 audio->config.in.codec = 0;
345 hb_log( "scan: unknown audio codec (%x)",
354 /* Check for duplicate tracks */
356 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
358 audio_tmp = hb_list_item( title->list_audio, j );
359 if( audio->id == audio_tmp->id )
367 hb_log( "scan: duplicate audio track" );
372 audio->config.lang.type = lang_extension;
374 lang = lang_for_code( vts->vtsi_mat->vts_audio_attr[i].lang_code );
376 snprintf( audio->config.lang.description, sizeof( audio->config.lang.description ), "%s (%s)",
377 strlen(lang->native_name) ? lang->native_name : lang->eng_name,
378 audio->config.in.codec == HB_ACODEC_AC3 ? "AC3" : ( audio->config.in.codec ==
379 HB_ACODEC_DCA ? "DTS" : ( audio->config.in.codec ==
380 HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) ) );
381 snprintf( audio->config.lang.simple, sizeof( audio->config.lang.simple ), "%s",
382 strlen(lang->native_name) ? lang->native_name : lang->eng_name );
383 snprintf( audio->config.lang.iso639_2, sizeof( audio->config.lang.iso639_2 ), "%s",
386 switch( lang_extension )
392 strcat( audio->config.lang.description, " (Visually Impaired)" );
395 strcat( audio->config.lang.description, " (Director's Commentary 1)" );
398 strcat( audio->config.lang.description, " (Director's Commentary 2)" );
404 hb_log( "scan: id=%x, lang=%s, 3cc=%s ext=%i", audio->id,
405 audio->config.lang.description, audio->config.lang.iso639_2,
408 audio->config.in.track = i;
409 hb_list_add( title->list_audio, audio );
412 if( !hb_list_count( title->list_audio ) )
414 hb_log( "scan: ignoring title (no audio track)" );
418 memcpy( title->palette,
419 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette,
420 16 * sizeof( uint32_t ) );
422 /* Check for subtitles */
423 for( i = 0; i < vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
425 hb_subtitle_t * subtitle;
428 iso639_lang_t * lang;
429 int lang_extension = 0;
431 hb_log( "scan: checking subtitle %d", i + 1 );
434 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i];
436 if( !( spu_control & 0x80000000 ) )
438 hb_log( "scan: subtitle channel is not active" );
442 if( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
444 switch( vts->vtsi_mat->vts_video_attr.permitted_df )
447 position = spu_control & 0xFF;
450 position = ( spu_control >> 8 ) & 0xFF;
453 position = ( spu_control >> 16 ) & 0xFF;
458 position = ( spu_control >> 24 ) & 0x7F;
461 lang_extension = vts->vtsi_mat->vts_subp_attr[i].code_extension;
463 lang = lang_for_code( vts->vtsi_mat->vts_subp_attr[i].lang_code );
465 subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
466 subtitle->track = i+1;
467 subtitle->id = ( ( 0x20 + position ) << 8 ) | 0xbd;
468 snprintf( subtitle->lang, sizeof( subtitle->lang ), "%s",
469 strlen(lang->native_name) ? lang->native_name : lang->eng_name);
470 snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ), "%s",
472 subtitle->format = PICTURESUB;
473 subtitle->source = VOBSUB;
474 subtitle->dest = RENDERSUB; // By default render (burn-in) the VOBSUB.
476 subtitle->type = lang_extension;
478 switch( lang_extension )
485 strcat( subtitle->lang, " (Caption with bigger size character)");
488 strcat( subtitle->lang, " (Caption for Children)");
493 strcat( subtitle->lang, " (Closed Caption)");
496 strcat( subtitle->lang, " (Closed Caption with bigger size character)");
499 strcat( subtitle->lang, " (Closed Caption for Children)");
504 strcat( subtitle->lang, " (Forced Caption)");
513 strcat( subtitle->lang, " (Director's Commentary)");
516 strcat( subtitle->lang, " (Director's Commentary with bigger size character)");
519 strcat( subtitle->lang, " (Director's Commentary for Children)");
524 hb_log( "scan: id=%x, lang=%s, 3cc=%s", subtitle->id,
525 subtitle->lang, subtitle->iso639_2 );
527 hb_list_add( title->list_subtitle, subtitle );
531 hb_log( "scan: title %d has %d chapters", t,
532 vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts );
534 i < vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts; i++ )
536 int pgc_id_next, pgn_next;
539 chapter = calloc( sizeof( hb_chapter_t ), 1 );
540 /* remember the on-disc chapter number */
541 chapter->index = i + 1;
543 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgcn;
544 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgn;
545 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
548 chapter->cell_start = d->pgc->program_map[pgn-1] - 1;
549 chapter->block_start =
550 d->pgc->cell_playback[chapter->cell_start].first_sector;
553 if( i != vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts - 1 )
555 /* The cell before the starting cell of the next chapter,
557 pgc_id_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgcn;
558 pgn_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgn;
559 pgc_next = vts->vts_pgcit->pgci_srp[pgc_id_next-1].pgc;
560 chapter->cell_end = pgc_next->program_map[pgn_next-1] - 2;
561 if( chapter->cell_end < 0 )
570 /* ... the last cell of the title */
571 chapter->cell_end = title->cell_end;
574 d->pgc->cell_playback[chapter->cell_end].last_sector;
576 /* Block count, duration */
577 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
578 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
579 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
580 chapter->block_count = 0;
581 chapter->duration = 0;
583 d->cell_cur = chapter->cell_start;
584 while( d->cell_cur <= chapter->cell_end )
586 #define cp d->pgc->cell_playback[d->cell_cur]
587 chapter->block_count += cp.last_sector + 1 - cp.first_sector;
588 chapter->duration += 90LL * dvdtime2msec( &cp.playback_time );
591 d->cell_cur = d->cell_next;
593 hb_list_add( title->list_chapter, chapter );
597 /* The durations we get for chapters aren't precise. Scale them so
598 the total matches the title duration */
600 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
602 chapter = hb_list_item( title->list_chapter, i );
603 duration += chapter->duration;
605 duration_correction = (float) title->duration / (float) duration;
606 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
609 chapter = hb_list_item( title->list_chapter, i );
610 chapter->duration = duration_correction * chapter->duration;
611 seconds = ( chapter->duration + 45000 ) / 90000;
612 chapter->hours = seconds / 3600;
613 chapter->minutes = ( seconds % 3600 ) / 60;
614 chapter->seconds = seconds % 60;
616 hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %lld ms",
617 chapter->index, chapter->cell_start, chapter->cell_end,
618 chapter->block_start, chapter->block_end,
619 chapter->block_count, chapter->duration / 90 );
622 /* Get aspect. We don't get width/height/rate infos here as
623 they tend to be wrong */
624 switch( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
627 title->container_aspect = 4. / 3.;
630 title->container_aspect = 16. / 9.;
633 hb_log( "scan: unknown aspect" );
637 hb_log( "scan: aspect = %g", title->aspect );
639 /* This title is ok so far */
643 hb_list_close( &title->list_audio );
648 if( vts ) ifoClose( vts );
653 /***********************************************************************
655 ***********************************************************************
656 * Title and chapter start at 1
657 **********************************************************************/
658 static int hb_dvdread_start( hb_dvd_t * e, hb_title_t *title, int chapter )
660 hb_dvdread_t *d = &(e->dvdread);
663 int t = title->index;
665 /* Open the IFO and the VOBs for this title */
666 d->vts = d->vmg->tt_srpt->title[t-1].title_set_nr;
667 d->ttn = d->vmg->tt_srpt->title[t-1].vts_ttn;
668 if( !( d->ifo = ifoOpen( d->reader, d->vts ) ) )
670 hb_error( "dvd: ifoOpen failed for VTS %d", d->vts );
673 if( !( d->file = DVDOpenFile( d->reader, d->vts,
674 DVD_READ_TITLE_VOBS ) ) )
676 hb_error( "dvd: DVDOpenFile failed for VTS %d", d->vts );
680 /* Get title first and last blocks */
681 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgcn;
682 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgn;
683 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
684 d->cell_start = d->pgc->program_map[pgn - 1] - 1;
685 d->cell_end = d->pgc->nr_of_cells - 1;
686 d->title_start = d->pgc->cell_playback[d->cell_start].first_sector;
687 d->title_end = d->pgc->cell_playback[d->cell_end].last_sector;
690 d->title_block_count = 0;
691 for( i = d->cell_start; i <= d->cell_end; i++ )
693 d->title_block_count += d->pgc->cell_playback[i].last_sector + 1 -
694 d->pgc->cell_playback[i].first_sector;
697 /* Get pgc for the current chapter */
698 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgcn;
699 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgn;
700 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
702 /* Get the two first cells */
703 d->cell_cur = d->pgc->program_map[pgn-1] - 1;
706 d->block = d->pgc->cell_playback[d->cell_cur].first_sector;
707 d->next_vobu = d->block;
716 /***********************************************************************
718 ***********************************************************************
720 **********************************************************************/
721 static void hb_dvdread_stop( hb_dvd_t * e )
723 hb_dvdread_t *d = &(e->dvdread);
731 DVDCloseFile( d->file );
736 /***********************************************************************
738 ***********************************************************************
740 **********************************************************************/
741 static int hb_dvdread_seek( hb_dvd_t * e, float f )
743 hb_dvdread_t *d = &(e->dvdread);
747 count = f * d->title_block_count;
749 for( i = d->cell_start; i <= d->cell_end; i++ )
751 sizeCell = d->pgc->cell_playback[i].last_sector + 1 -
752 d->pgc->cell_playback[i].first_sector;
754 if( count < sizeCell )
760 /* Now let hb_dvdread_read find the next VOBU */
761 d->next_vobu = d->pgc->cell_playback[i].first_sector + count;
769 if( i > d->cell_end )
775 * Assume that we are in sync, even if we are not given that it is obvious
776 * that we are out of sync if we have just done a seek.
784 /***********************************************************************
786 ***********************************************************************/
787 int is_nav_pack( unsigned char *buf )
790 * The NAV Pack is comprised of the PCI Packet and DSI Packet, both
791 * of these start at known offsets and start with a special identifier.
794 * PCI = { 00 00 01 bf # private stream header
799 * DSI = { 00 00 01 bf # private stream header
805 * The PCI starts at offset 0x26 into the sector, and the DSI starts at 0x400
807 * This information from: http://dvd.sourceforge.net/dvdinfo/
809 if( ( buf[0x26] == 0x00 && // PCI
813 buf[0x2c] == 0x00 ) &&
814 ( buf[0x400] == 0x00 && // DSI
815 buf[0x401] == 0x00 &&
816 buf[0x402] == 0x01 &&
817 buf[0x403] == 0xbf &&
818 buf[0x406] == 0x01 ) )
827 /***********************************************************************
829 ***********************************************************************
831 **********************************************************************/
832 static int hb_dvdread_read( hb_dvd_t * e, hb_buffer_t * b )
834 hb_dvdread_t *d = &(e->dvdread);
842 // if we've just finished the last cell of the title we don't
843 // want to read another block because our next_vobu pointer
844 // is probably invalid. Just return 'no data' & our caller
845 // should check and discover we're at eof.
846 if ( d->cell_cur > d->cell_end )
851 int block, pack_len, next_vobu, read_retry;
853 for( read_retry = 0; read_retry < 3; read_retry++ )
855 if( DVDReadBlocks( d->file, d->next_vobu, 1, b->data ) == 1 )
862 hb_log( "dvd: Read Error on blk %d, attempt %d",
863 d->next_vobu, read_retry );
867 if( read_retry == 3 )
869 hb_log( "dvd: vobu read error blk %d - skipping to cell %d",
870 d->next_vobu, d->cell_next );
871 d->cell_cur = d->cell_next;
872 if ( d->cell_cur > d->cell_end )
875 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
881 if ( !is_nav_pack( b->data ) ) {
883 if( d->in_sync == 1 ) {
884 hb_log("dvd: Lost sync, searching for NAV pack at blk %d",
891 navRead_DSI( &dsi_pack, &b->data[DSI_START_BYTE] );
893 if ( d->in_sync == 0 && d->cur_cell_id &&
894 (d->cur_vob_id != dsi_pack.dsi_gi.vobu_vob_idn ||
895 d->cur_cell_id != dsi_pack.dsi_gi.vobu_c_idn ) )
897 // We walked out of the cell we're supposed to be in.
898 // If we're not at the start of our next cell go there.
899 hb_log("dvd: left cell %d (%u,%u) for (%u,%u) at block %u",
900 d->cell_cur, d->cur_vob_id, d->cur_cell_id,
901 dsi_pack.dsi_gi.vobu_vob_idn, dsi_pack.dsi_gi.vobu_c_idn,
903 if ( d->next_vobu != d->pgc->cell_playback[d->cell_next].first_sector )
905 d->next_vobu = d->pgc->cell_playback[d->cell_next].first_sector;
911 block = dsi_pack.dsi_gi.nv_pck_lbn;
912 pack_len = dsi_pack.dsi_gi.vobu_ea;
914 // There are a total of 21 next vobu offsets (and 21 prev_vobu
915 // offsets) in the navpack SRI structure. The primary one is
916 // 'next_vobu' which is the offset (in dvd blocks) from the current
917 // block to the start of the next vobu. If the block at 'next_vobu'
918 // can't be read, 'next_video' is the offset to the vobu closest to it.
919 // The other 19 offsets are vobus at successively longer distances from
920 // the current block (this is so that a hardware player can do
921 // adaptive error recovery to skip over a bad spot on the disk). In all
922 // these offsets the high bit is set to indicate when it contains a
923 // valid offset. The next bit (2^30) is set to indicate that there's
924 // another valid offset in the SRI that's closer to the current block.
925 // A hardware player uses these bits to chose the closest valid offset
926 // and uses that as its next vobu. (Some mastering schemes appear to
927 // put a bogus offset in next_vobu with the 2^30 bit set & the
928 // correct offset in next_video. This works fine in hardware players
929 // but will mess up software that doesn't implement the full next
930 // vobu decision logic.) In addition to the flag bits there's a
931 // reserved value of the offset that indicates 'no next vobu' (which
932 // indicates the end of a cell). But having no next vobu pointer with a
933 // 'valid' bit set also indicates end of cell. Different mastering
934 // schemes seem to use all possible combinations of the flag bits
935 // and reserved values to indicate end of cell so we have to check
936 // them all or we'll get a disk read error from following an
938 uint32_t next_ptr = dsi_pack.vobu_sri.next_vobu;
939 if ( ( next_ptr & ( 1 << 31 ) ) == 0 ||
940 ( next_ptr & ( 1 << 30 ) ) != 0 ||
941 ( next_ptr & 0x3fffffff ) == 0x3fffffff )
943 next_ptr = dsi_pack.vobu_sri.next_video;
944 if ( ( next_ptr & ( 1 << 31 ) ) == 0 ||
945 ( next_ptr & 0x3fffffff ) == 0x3fffffff )
947 // don't have a valid forward pointer - assume end-of-cell
949 d->pack_len = pack_len;
953 next_vobu = block + ( next_ptr & 0x3fffffff );
957 block >= d->next_vobu &&
958 ( block <= d->title_start + d->title_block_count ||
959 block <= d->title_end ) )
962 d->pack_len = pack_len;
963 d->next_vobu = next_vobu;
967 /* Wasn't a valid VOBU, try next block */
970 hb_log( "dvd: couldn't find a VOBU after 1024 blocks" );
977 if( d->in_sync == 0 || d->in_sync == 2 )
979 if( d->in_sync == 0 )
981 hb_log( "dvd: In sync with DVD at block %d", d->block );
986 // Revert the cell overlap, and check for a chapter break
987 // If this cell is zero length (prev_vobu & next_vobu both
988 // set to END_OF_CELL) we need to check for beginning of
989 // cell before checking for end or we'll advance to the next
990 // cell too early and fail to generate a chapter mark when this
991 // cell starts a chapter.
992 if( ( dsi_pack.vobu_sri.prev_vobu & (1 << 31 ) ) == 0 ||
993 ( dsi_pack.vobu_sri.prev_vobu & 0x3fffffff ) == 0x3fffffff )
995 // A vobu that's not at the start of a cell can have an
996 // EOC prev pointer (this seems to be associated with some
997 // sort of drm). The rest of the content in the cell may be
998 // booby-trapped so treat this like an end-of-cell rather
999 // than a beginning of cell.
1000 if ( d->pgc->cell_playback[d->cell_cur].first_sector < dsi_pack.dsi_gi.nv_pck_lbn &&
1001 d->pgc->cell_playback[d->cell_cur].last_sector >= dsi_pack.dsi_gi.nv_pck_lbn )
1003 hb_log( "dvd: null prev_vobu in cell %d at block %d", d->cell_cur,
1005 // treat like end-of-cell then go directly to start of next cell.
1006 d->cell_cur = d->cell_next;
1008 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
1010 d->cell_overlap = 1;
1015 if ( d->block != d->pgc->cell_playback[d->cell_cur].first_sector )
1017 hb_log( "dvd: beginning of cell %d at block %d", d->cell_cur,
1022 hb_error( "dvd: assuming missed end of cell %d", d->cell_cur );
1023 d->cell_cur = d->cell_next;
1024 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
1026 d->cell_overlap = 1;
1031 d->cur_vob_id = dsi_pack.dsi_gi.vobu_vob_idn;
1032 d->cur_cell_id = dsi_pack.dsi_gi.vobu_c_idn;
1034 if( d->cell_overlap )
1036 b->new_chap = hb_dvdread_is_break( d );
1037 d->cell_overlap = 0;
1042 if( ( dsi_pack.vobu_sri.next_vobu & (1 << 31 ) ) == 0 ||
1043 ( dsi_pack.vobu_sri.next_vobu & 0x3fffffff ) == 0x3fffffff )
1045 if ( d->block <= d->pgc->cell_playback[d->cell_cur].first_sector ||
1046 d->block > d->pgc->cell_playback[d->cell_cur].last_sector )
1048 hb_log( "dvd: end of cell %d at block %d", d->cell_cur,
1051 d->cell_cur = d->cell_next;
1053 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
1055 d->cell_overlap = 1;
1061 if( DVDReadBlocks( d->file, d->block, 1, b->data ) != 1 )
1063 // this may be a real DVD error or may be DRM. Either way
1064 // we don't want to quit because of one bad block so set
1065 // things up so we'll advance to the next vobu and recurse.
1066 hb_error( "dvd: DVDReadBlocks failed (%d), skipping to vobu %u",
1067 d->block, d->next_vobu );
1069 goto top; /* XXX need to restructure this routine & avoid goto */
1079 /***********************************************************************
1080 * hb_dvdread_chapter
1081 ***********************************************************************
1082 * Returns in which chapter the next block to be read is.
1083 * Chapter numbers start at 1.
1084 **********************************************************************/
1085 static int hb_dvdread_chapter( hb_dvd_t * e )
1087 hb_dvdread_t *d = &(e->dvdread);
1090 int nr_of_ptts = d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
1093 for( i = nr_of_ptts - 1;
1097 /* Get pgc for chapter (i+1) */
1098 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
1099 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
1100 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1102 if( d->cell_cur - d->cell_overlap >= pgc->program_map[pgn-1] - 1 &&
1103 d->cell_cur - d->cell_overlap <= pgc->nr_of_cells - 1 )
1105 /* We are in this chapter */
1114 /***********************************************************************
1115 * hb_dvdread_is_break
1116 ***********************************************************************
1117 * Returns chapter number if the current block is a new chapter start
1118 **********************************************************************/
1119 static int hb_dvdread_is_break( hb_dvdread_t * d )
1123 int nr_of_ptts = d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
1127 for( i = nr_of_ptts - 1;
1131 /* Get pgc for chapter (i+1) */
1132 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
1133 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
1134 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1135 cell = pgc->program_map[pgn-1] - 1;
1137 if( cell <= d->cell_start )
1140 // This must not match against the start cell.
1141 if( pgc->cell_playback[cell].first_sector == d->block && cell != d->cell_start )
1150 /***********************************************************************
1152 ***********************************************************************
1153 * Closes and frees everything
1154 **********************************************************************/
1155 static void hb_dvdread_close( hb_dvd_t ** _d )
1157 hb_dvdread_t * d = &((*_d)->dvdread);
1165 DVDClose( d->reader );
1172 /***********************************************************************
1173 * hb_dvdread_angle_count
1174 ***********************************************************************
1175 * Returns the number of angles supported. We do not support angles
1177 **********************************************************************/
1178 static int hb_dvdread_angle_count( hb_dvd_t * d )
1183 /***********************************************************************
1184 * hb_dvdread_set_angle
1185 ***********************************************************************
1186 * Sets the angle to read. Not supported with dvdread
1187 **********************************************************************/
1188 static void hb_dvdread_set_angle( hb_dvd_t * d, int angle )
1192 /***********************************************************************
1194 ***********************************************************************
1195 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1196 * cell to be read when we will be done with cell_cur.
1197 **********************************************************************/
1198 static void FindNextCell( hb_dvdread_t * d )
1202 if( d->pgc->cell_playback[d->cell_cur].block_type ==
1203 BLOCK_TYPE_ANGLE_BLOCK )
1206 while( d->pgc->cell_playback[d->cell_cur+i].block_mode !=
1207 BLOCK_MODE_LAST_CELL )
1211 d->cell_next = d->cell_cur + i + 1;
1212 hb_log( "dvd: Skipping multi-angle cells %d-%d",
1218 d->cell_next = d->cell_cur + 1;
1222 /***********************************************************************
1224 ***********************************************************************
1226 **********************************************************************/
1227 static int dvdtime2msec(dvd_time_t * dt)
1229 double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
1230 double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
1232 ms = (((dt->hour & 0xf0) >> 3) * 5 + (dt->hour & 0x0f)) * 3600000;
1233 ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
1234 ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
1238 ms += ((dt->frame_u & 0x30) >> 3) * 5 +
1239 (dt->frame_u & 0x0f) * 1000.0 / fps;
1245 char * hb_dvd_name( char * path )
1247 return dvd_methods->name(path);
1250 hb_dvd_t * hb_dvd_init( char * path )
1252 return dvd_methods->init(path);
1255 int hb_dvd_title_count( hb_dvd_t * d )
1257 return dvd_methods->title_count(d);
1260 hb_title_t * hb_dvd_title_scan( hb_dvd_t * d, int t )
1262 return dvd_methods->title_scan(d, t);
1265 int hb_dvd_start( hb_dvd_t * d, hb_title_t *title, int chapter )
1267 return dvd_methods->start(d, title, chapter);
1270 void hb_dvd_stop( hb_dvd_t * d )
1272 dvd_methods->stop(d);
1275 int hb_dvd_seek( hb_dvd_t * d, float f )
1277 return dvd_methods->seek(d, f);
1280 int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
1282 return dvd_methods->read(d, b);
1285 int hb_dvd_chapter( hb_dvd_t * d )
1287 return dvd_methods->chapter(d);
1290 void hb_dvd_close( hb_dvd_t ** _d )
1292 dvd_methods->close(_d);
1295 int hb_dvd_angle_count( hb_dvd_t * d )
1297 return dvd_methods->angle_count(d);
1300 void hb_dvd_set_angle( hb_dvd_t * d, int angle )
1302 dvd_methods->set_angle(d, angle);
1305 // hb_dvd_set_dvdnav must only be called when no dvd source is open
1306 // it rips the rug out from under things so be careful
1307 void hb_dvd_set_dvdnav( int enable )
1310 dvd_methods = hb_dvdnav_methods();
1312 dvd_methods = hb_dvdread_methods();