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. */
10 #include "dvdread/ifo_read.h"
11 #include "dvdread/nav_read.h"
17 dvd_reader_t * reader;
30 int title_block_count;
43 /***********************************************************************
45 **********************************************************************/
46 static void FindNextCell( hb_dvd_t * );
47 static int dvdtime2msec( dvd_time_t * );
49 char * hb_dvd_name( char * path )
51 static char name[1024];
52 unsigned char unused[1024];
53 dvd_reader_t * reader;
55 reader = DVDOpen( path );
61 if( DVDUDFVolumeInfo( reader, name, sizeof( name ),
62 unused, sizeof( unused ) ) )
72 /***********************************************************************
74 ***********************************************************************
76 **********************************************************************/
77 hb_dvd_t * hb_dvd_init( char * path )
81 d = calloc( sizeof( hb_dvd_t ), 1 );
84 if( !( d->reader = DVDOpen( path ) ) )
87 * Not an error, may be a stream - which we'll try in a moment.
89 hb_log( "dvd: not a dvd - trying as a stream/file instead" );
94 if( !( d->vmg = ifoOpen( d->reader, 0 ) ) )
96 hb_error( "dvd: ifoOpen failed" );
100 d->path = strdup( path );
105 if( d->vmg ) ifoClose( d->vmg );
106 if( d->reader ) DVDClose( d->reader );
111 /***********************************************************************
113 **********************************************************************/
114 int hb_dvd_title_count( hb_dvd_t * d )
116 return d->vmg->tt_srpt->nr_of_srpts;
119 /***********************************************************************
121 **********************************************************************/
122 hb_title_t * hb_dvd_title_scan( hb_dvd_t * d, int t )
126 ifo_handle_t * vts = NULL;
128 hb_chapter_t * chapter;
131 float duration_correction;
132 unsigned char unused[1024];
134 hb_log( "scan: scanning title %d", t );
136 title = hb_title_init( d->path, t );
137 if( DVDUDFVolumeInfo( d->reader, title->name, sizeof( title->name ),
138 unused, sizeof( unused ) ) )
140 char * p_cur, * p_last = d->path;
141 for( p_cur = d->path; *p_cur; p_cur++ )
143 if( p_cur[0] == '/' && p_cur[1] )
148 snprintf( title->name, sizeof( title->name ), "%s", p_last );
151 /* VTS which our title is in */
152 title->vts = d->vmg->tt_srpt->title[t-1].title_set_nr;
156 /* A VTS of 0 means the title wasn't found in the title set */
157 hb_error("Invalid VTS (title set) number: %i", title->vts);
161 hb_log( "scan: opening IFO for VTS %d", title->vts );
162 if( !( vts = ifoOpen( d->reader, title->vts ) ) )
164 hb_error( "scan: ifoOpen failed" );
168 if( global_verbosity_level == 3 )
170 ifoPrint( d->reader, title->vts );
173 /* Position of the title in the VTS */
174 title->ttn = d->vmg->tt_srpt->title[t-1].vts_ttn;
177 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
178 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
179 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
181 hb_log("pgc_id: %d, pgn: %d: pgc: 0x%x", pgc_id, pgn, d->pgc);
185 hb_error( "scan: pgc not valid, skipping" );
189 if( pgn <= 0 || pgn > 99 )
191 hb_error( "scan: pgn %d not valid, skipping", pgn );
196 title->cell_start = d->pgc->program_map[pgn-1] - 1;
197 title->block_start = d->pgc->cell_playback[title->cell_start].first_sector;
200 title->cell_end = d->pgc->nr_of_cells - 1;
201 title->block_end = d->pgc->cell_playback[title->cell_end].last_sector;
204 title->block_count = 0;
205 d->cell_cur = title->cell_start;
206 while( d->cell_cur <= title->cell_end )
208 #define cp d->pgc->cell_playback[d->cell_cur]
209 title->block_count += cp.last_sector + 1 - cp.first_sector;
212 d->cell_cur = d->cell_next;
215 hb_log( "scan: vts=%d, ttn=%d, cells=%d->%d, blocks=%d->%d, "
216 "%d blocks", title->vts, title->ttn, title->cell_start,
217 title->cell_end, title->block_start, title->block_end,
218 title->block_count );
221 title->duration = 90LL * dvdtime2msec( &d->pgc->playback_time );
222 title->hours = title->duration / 90000 / 3600;
223 title->minutes = ( ( title->duration / 90000 ) % 3600 ) / 60;
224 title->seconds = ( title->duration / 90000 ) % 60;
225 hb_log( "scan: duration is %02d:%02d:%02d (%lld ms)",
226 title->hours, title->minutes, title->seconds,
227 title->duration / 90 );
229 /* ignore titles under 10 seconds because they're often stills or
230 * clips with no audio & our preview code doesn't currently handle
231 * either of these. */
232 if( title->duration < 900000LL )
234 hb_log( "scan: ignoring title (too short)" );
238 /* ignore titles with bogus cell addresses so we don't abort later
240 for ( i = 0; i < vts->vts_c_adt->nr_of_vobs; ++i)
242 if( (vts->vts_c_adt->cell_adr_table[i].start_sector & 0xffffff ) ==
245 hb_error( "scan: cell_adr_table[%d].start_sector invalid (0x%x) "
246 "- skipping title", i,
247 vts->vts_c_adt->cell_adr_table[i].start_sector );
250 if( (vts->vts_c_adt->cell_adr_table[i].last_sector & 0xffffff ) ==
253 hb_error( "scan: cell_adr_table[%d].last_sector invalid (0x%x) "
254 "- skipping title", i,
255 vts->vts_c_adt->cell_adr_table[i].last_sector );
258 if( vts->vts_c_adt->cell_adr_table[i].start_sector >=
259 vts->vts_c_adt->cell_adr_table[i].last_sector )
261 hb_error( "scan: cell_adr_table[%d].start_sector (0x%x) "
262 "is not before last_sector (0x%x) - skipping title", i,
263 vts->vts_c_adt->cell_adr_table[i].start_sector,
264 vts->vts_c_adt->cell_adr_table[i].last_sector );
269 /* Detect languages */
270 for( i = 0; i < vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
272 hb_audio_t * audio, * audio_tmp;
273 int audio_format, lang_code, audio_control,
275 iso639_lang_t * lang;
276 int lang_extension = 0;
278 hb_log( "scan: checking audio %d", i + 1 );
280 audio = calloc( sizeof( hb_audio_t ), 1 );
282 audio_format = vts->vtsi_mat->vts_audio_attr[i].audio_format;
283 lang_code = vts->vtsi_mat->vts_audio_attr[i].lang_code;
284 lang_extension = vts->vtsi_mat->vts_audio_attr[i].code_extension;
286 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i];
288 if( !( audio_control & 0x8000 ) )
290 hb_log( "scan: audio channel is not active" );
295 position = ( audio_control & 0x7F00 ) >> 8;
297 switch( audio_format )
300 audio->id = ( ( 0x80 + position ) << 8 ) | 0xbd;
301 audio->config.in.codec = HB_ACODEC_AC3;
306 audio->id = 0xc0 + position;
307 audio->config.in.codec = HB_ACODEC_MPGA;
311 audio->id = ( ( 0xa0 + position ) << 8 ) | 0xbd;
312 audio->config.in.codec = HB_ACODEC_LPCM;
316 audio->id = ( ( 0x88 + position ) << 8 ) | 0xbd;
317 audio->config.in.codec = HB_ACODEC_DCA;
322 audio->config.in.codec = 0;
323 hb_log( "scan: unknown audio codec (%x)",
332 /* Check for duplicate tracks */
334 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
336 audio_tmp = hb_list_item( title->list_audio, j );
337 if( audio->id == audio_tmp->id )
345 hb_log( "scan: duplicate audio track" );
350 audio->config.lang.type = lang_extension;
352 lang = lang_for_code( vts->vtsi_mat->vts_audio_attr[i].lang_code );
354 snprintf( audio->config.lang.description, sizeof( audio->config.lang.description ), "%s (%s)",
355 strlen(lang->native_name) ? lang->native_name : lang->eng_name,
356 audio->config.in.codec == HB_ACODEC_AC3 ? "AC3" : ( audio->config.in.codec ==
357 HB_ACODEC_DCA ? "DTS" : ( audio->config.in.codec ==
358 HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) ) );
359 snprintf( audio->config.lang.simple, sizeof( audio->config.lang.simple ), "%s",
360 strlen(lang->native_name) ? lang->native_name : lang->eng_name );
361 snprintf( audio->config.lang.iso639_2, sizeof( audio->config.lang.iso639_2 ), "%s",
364 switch( lang_extension )
370 strcat( audio->config.lang.description, " (Visually Impaired)" );
373 strcat( audio->config.lang.description, " (Director's Commentary 1)" );
376 strcat( audio->config.lang.description, " (Director's Commentary 2)" );
382 hb_log( "scan: id=%x, lang=%s, 3cc=%s ext=%i", audio->id,
383 audio->config.lang.description, audio->config.lang.iso639_2,
386 audio->config.in.track = i;
387 hb_list_add( title->list_audio, audio );
390 if( !hb_list_count( title->list_audio ) )
392 hb_log( "scan: ignoring title (no audio track)" );
396 memcpy( title->palette,
397 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette,
398 16 * sizeof( uint32_t ) );
400 /* Check for subtitles */
401 for( i = 0; i < vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
403 hb_subtitle_t * subtitle;
406 iso639_lang_t * lang;
407 int lang_extension = 0;
409 hb_log( "scan: checking subtitle %d", i + 1 );
412 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i];
414 if( !( spu_control & 0x80000000 ) )
416 hb_log( "scan: subtitle channel is not active" );
420 if( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
422 switch( vts->vtsi_mat->vts_video_attr.permitted_df )
425 position = spu_control & 0xFF;
428 position = ( spu_control >> 8 ) & 0xFF;
431 position = ( spu_control >> 16 ) & 0xFF;
436 position = ( spu_control >> 24 ) & 0x7F;
439 lang_extension = vts->vtsi_mat->vts_subp_attr[i].code_extension;
441 lang = lang_for_code( vts->vtsi_mat->vts_subp_attr[i].lang_code );
443 subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
444 subtitle->id = ( ( 0x20 + position ) << 8 ) | 0xbd;
445 snprintf( subtitle->lang, sizeof( subtitle->lang ), "%s",
446 strlen(lang->native_name) ? lang->native_name : lang->eng_name);
447 snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ), "%s",
450 subtitle->type = lang_extension;
452 switch( lang_extension )
459 strcat( subtitle->lang, " (Caption with bigger size character)");
462 strcat( subtitle->lang, " (Caption for Children)");
467 strcat( subtitle->lang, " (Closed Caption)");
470 strcat( subtitle->lang, " (Closed Caption with bigger size character)");
473 strcat( subtitle->lang, " (Closed Caption for Children)");
478 strcat( subtitle->lang, " (Forced Caption)");
487 strcat( subtitle->lang, " (Director's Commentary)");
490 strcat( subtitle->lang, " (Director's Commentary with bigger size character)");
493 strcat( subtitle->lang, " (Director's Commentary for Children)");
498 hb_log( "scan: id=%x, lang=%s, 3cc=%s", subtitle->id,
499 subtitle->lang, subtitle->iso639_2 );
501 hb_list_add( title->list_subtitle, subtitle );
505 hb_log( "scan: title %d has %d chapters", t,
506 vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts );
508 i < vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts; i++ )
510 int pgc_id_next, pgn_next;
513 chapter = calloc( sizeof( hb_chapter_t ), 1 );
514 /* remember the on-disc chapter number */
515 chapter->index = i + 1;
517 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgcn;
518 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgn;
519 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
522 chapter->cell_start = d->pgc->program_map[pgn-1] - 1;
523 chapter->block_start =
524 d->pgc->cell_playback[chapter->cell_start].first_sector;
527 if( i != vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts - 1 )
529 /* The cell before the starting cell of the next chapter,
531 pgc_id_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgcn;
532 pgn_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgn;
533 pgc_next = vts->vts_pgcit->pgci_srp[pgc_id_next-1].pgc;
534 chapter->cell_end = pgc_next->program_map[pgn_next-1] - 2;
535 if( chapter->cell_end < 0 )
544 /* ... the last cell of the title */
545 chapter->cell_end = title->cell_end;
548 d->pgc->cell_playback[chapter->cell_end].last_sector;
550 /* Block count, duration */
551 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
552 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
553 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
554 chapter->block_count = 0;
555 chapter->duration = 0;
557 d->cell_cur = chapter->cell_start;
558 while( d->cell_cur <= chapter->cell_end )
560 #define cp d->pgc->cell_playback[d->cell_cur]
561 chapter->block_count += cp.last_sector + 1 - cp.first_sector;
562 chapter->duration += 90LL * dvdtime2msec( &cp.playback_time );
565 d->cell_cur = d->cell_next;
567 hb_list_add( title->list_chapter, chapter );
571 /* The durations we get for chapters aren't precise. Scale them so
572 the total matches the title duration */
574 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
576 chapter = hb_list_item( title->list_chapter, i );
577 duration += chapter->duration;
579 duration_correction = (float) title->duration / (float) duration;
580 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
583 chapter = hb_list_item( title->list_chapter, i );
584 chapter->duration = duration_correction * chapter->duration;
585 seconds = ( chapter->duration + 45000 ) / 90000;
586 chapter->hours = seconds / 3600;
587 chapter->minutes = ( seconds % 3600 ) / 60;
588 chapter->seconds = seconds % 60;
590 hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %lld ms",
591 chapter->index, chapter->cell_start, chapter->cell_end,
592 chapter->block_start, chapter->block_end,
593 chapter->block_count, chapter->duration / 90 );
596 /* Get aspect. We don't get width/height/rate infos here as
597 they tend to be wrong */
598 switch( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
601 title->container_aspect = 4. / 3.;
604 title->container_aspect = 16. / 9.;
607 hb_log( "scan: unknown aspect" );
611 hb_log( "scan: aspect = %g", title->aspect );
613 /* This title is ok so far */
617 hb_list_close( &title->list_audio );
622 if( vts ) ifoClose( vts );
627 /***********************************************************************
629 ***********************************************************************
630 * Title and chapter start at 1
631 **********************************************************************/
632 int hb_dvd_start( hb_dvd_t * d, int title, int chapter )
637 /* Open the IFO and the VOBs for this title */
638 d->vts = d->vmg->tt_srpt->title[title-1].title_set_nr;
639 d->ttn = d->vmg->tt_srpt->title[title-1].vts_ttn;
640 if( !( d->ifo = ifoOpen( d->reader, d->vts ) ) )
642 hb_error( "dvd: ifoOpen failed for VTS %d", d->vts );
645 if( !( d->file = DVDOpenFile( d->reader, d->vts,
646 DVD_READ_TITLE_VOBS ) ) )
648 hb_error( "dvd: DVDOpenFile failed for VTS %d", d->vts );
652 /* Get title first and last blocks */
653 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgcn;
654 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgn;
655 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
656 d->cell_start = d->pgc->program_map[pgn - 1] - 1;
657 d->cell_end = d->pgc->nr_of_cells - 1;
658 d->title_start = d->pgc->cell_playback[d->cell_start].first_sector;
659 d->title_end = d->pgc->cell_playback[d->cell_end].last_sector;
662 d->title_block_count = 0;
663 for( i = d->cell_start; i <= d->cell_end; i++ )
665 d->title_block_count += d->pgc->cell_playback[i].last_sector + 1 -
666 d->pgc->cell_playback[i].first_sector;
669 /* Get pgc for the current chapter */
670 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgcn;
671 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgn;
672 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
674 /* Get the two first cells */
675 d->cell_cur = d->pgc->program_map[pgn-1] - 1;
678 d->block = d->pgc->cell_playback[d->cell_cur].first_sector;
679 d->next_vobu = d->block;
688 /***********************************************************************
690 ***********************************************************************
692 **********************************************************************/
693 void hb_dvd_stop( hb_dvd_t * d )
702 DVDCloseFile( d->file );
707 /***********************************************************************
709 ***********************************************************************
711 **********************************************************************/
712 int hb_dvd_seek( hb_dvd_t * d, float f )
717 count = f * d->title_block_count;
719 for( i = d->cell_start; i <= d->cell_end; i++ )
721 sizeCell = d->pgc->cell_playback[i].last_sector + 1 -
722 d->pgc->cell_playback[i].first_sector;
724 if( count < sizeCell )
730 /* Now let hb_dvd_read find the next VOBU */
731 d->next_vobu = d->pgc->cell_playback[i].first_sector + count;
739 if( i > d->cell_end )
745 * Assume that we are in sync, even if we are not given that it is obvious
746 * that we are out of sync if we have just done a seek.
754 /***********************************************************************
756 ***********************************************************************/
757 int is_nav_pack( unsigned char *buf )
760 * The NAV Pack is comprised of the PCI Packet and DSI Packet, both
761 * of these start at known offsets and start with a special identifier.
764 * PCI = { 00 00 01 bf # private stream header
769 * DSI = { 00 00 01 bf # private stream header
775 * The PCI starts at offset 0x26 into the sector, and the DSI starts at 0x400
777 * This information from: http://dvd.sourceforge.net/dvdinfo/
779 if( ( buf[0x26] == 0x00 && // PCI
783 buf[0x2c] == 0x00 ) &&
784 ( buf[0x400] == 0x00 && // DSI
785 buf[0x401] == 0x00 &&
786 buf[0x402] == 0x01 &&
787 buf[0x403] == 0xbf &&
788 buf[0x406] == 0x01 ) )
797 /***********************************************************************
799 ***********************************************************************
801 **********************************************************************/
802 int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
811 // if we've just finished the last cell of the title we don't
812 // want to read another block because our next_vobu pointer
813 // is probably invalid. Just return 'no data' & our caller
814 // should check and discover we're at eof.
815 if ( d->cell_cur > d->cell_end )
820 int block, pack_len, next_vobu, read_retry;
822 for( read_retry = 0; read_retry < 3; read_retry++ )
824 if( DVDReadBlocks( d->file, d->next_vobu, 1, b->data ) == 1 )
831 hb_log( "dvd: Read Error on blk %d, attempt %d",
832 d->next_vobu, read_retry );
836 if( read_retry == 3 )
838 hb_log( "dvd: vobu read error blk %d - skipping to cell %d",
839 d->next_vobu, d->cell_next );
840 d->cell_cur = d->cell_next;
841 if ( d->cell_cur > d->cell_end )
844 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
850 if ( !is_nav_pack( b->data ) ) {
852 if( d->in_sync == 1 ) {
853 hb_log("dvd: Lost sync, searching for NAV pack at blk %d",
860 navRead_DSI( &dsi_pack, &b->data[DSI_START_BYTE] );
862 if ( d->in_sync == 0 && d->cur_cell_id &&
863 (d->cur_vob_id != dsi_pack.dsi_gi.vobu_vob_idn ||
864 d->cur_cell_id != dsi_pack.dsi_gi.vobu_c_idn ) )
866 // We walked out of the cell we're supposed to be in.
867 // If we're not at the start of our next cell go there.
868 hb_log("dvd: left cell %d (%u,%u) for (%u,%u) at block %u",
869 d->cell_cur, d->cur_vob_id, d->cur_cell_id,
870 dsi_pack.dsi_gi.vobu_vob_idn, dsi_pack.dsi_gi.vobu_c_idn,
872 if ( d->next_vobu != d->pgc->cell_playback[d->cell_next].first_sector )
874 d->next_vobu = d->pgc->cell_playback[d->cell_next].first_sector;
880 block = dsi_pack.dsi_gi.nv_pck_lbn;
881 pack_len = dsi_pack.dsi_gi.vobu_ea;
883 // There are a total of 21 next vobu offsets (and 21 prev_vobu
884 // offsets) in the navpack SRI structure. The primary one is
885 // 'next_vobu' which is the offset (in dvd blocks) from the current
886 // block to the start of the next vobu. If the block at 'next_vobu'
887 // can't be read, 'next_video' is the offset to the vobu closest to it.
888 // The other 19 offsets are vobus at successively longer distances from
889 // the current block (this is so that a hardware player can do
890 // adaptive error recovery to skip over a bad spot on the disk). In all
891 // these offsets the high bit is set to indicate when it contains a
892 // valid offset. The next bit (2^30) is set to indicate that there's
893 // another valid offset in the SRI that's closer to the current block.
894 // A hardware player uses these bits to chose the closest valid offset
895 // and uses that as its next vobu. (Some mastering schemes appear to
896 // put a bogus offset in next_vobu with the 2^30 bit set & the
897 // correct offset in next_video. This works fine in hardware players
898 // but will mess up software that doesn't implement the full next
899 // vobu decision logic.) In addition to the flag bits there's a
900 // reserved value of the offset that indicates 'no next vobu' (which
901 // indicates the end of a cell). But having no next vobu pointer with a
902 // 'valid' bit set also indicates end of cell. Different mastering
903 // schemes seem to use all possible combinations of the flag bits
904 // and reserved values to indicate end of cell so we have to check
905 // them all or we'll get a disk read error from following an
907 uint32_t next_ptr = dsi_pack.vobu_sri.next_vobu;
908 if ( ( next_ptr & ( 1 << 31 ) ) == 0 ||
909 ( next_ptr & ( 1 << 30 ) ) != 0 ||
910 ( next_ptr & 0x3fffffff ) == 0x3fffffff )
912 next_ptr = dsi_pack.vobu_sri.next_video;
913 if ( ( next_ptr & ( 1 << 31 ) ) == 0 ||
914 ( next_ptr & 0x3fffffff ) == 0x3fffffff )
916 // don't have a valid forward pointer - assume end-of-cell
918 d->pack_len = pack_len;
922 next_vobu = block + ( next_ptr & 0x3fffffff );
926 block >= d->next_vobu &&
927 ( block <= d->title_start + d->title_block_count ||
928 block <= d->title_end ) )
931 d->pack_len = pack_len;
932 d->next_vobu = next_vobu;
936 /* Wasn't a valid VOBU, try next block */
939 hb_log( "dvd: couldn't find a VOBU after 1024 blocks" );
946 if( d->in_sync == 0 || d->in_sync == 2 )
948 if( d->in_sync == 0 )
950 hb_log( "dvd: In sync with DVD at block %d", d->block );
955 // Revert the cell overlap, and check for a chapter break
956 // If this cell is zero length (prev_vobu & next_vobu both
957 // set to END_OF_CELL) we need to check for beginning of
958 // cell before checking for end or we'll advance to the next
959 // cell too early and fail to generate a chapter mark when this
960 // cell starts a chapter.
961 if( ( dsi_pack.vobu_sri.prev_vobu & (1 << 31 ) ) == 0 ||
962 ( dsi_pack.vobu_sri.prev_vobu & 0x3fffffff ) == 0x3fffffff )
964 // A vobu that's not at the start of a cell can have an
965 // EOC prev pointer (this seems to be associated with some
966 // sort of drm). The rest of the content in the cell may be
967 // booby-trapped so treat this like an end-of-cell rather
968 // than a beginning of cell.
969 if ( d->pgc->cell_playback[d->cell_cur].first_sector < dsi_pack.dsi_gi.nv_pck_lbn &&
970 d->pgc->cell_playback[d->cell_cur].last_sector >= dsi_pack.dsi_gi.nv_pck_lbn )
972 hb_log( "dvd: null prev_vobu in cell %d at block %d", d->cell_cur,
974 // treat like end-of-cell then go directly to start of next cell.
975 d->cell_cur = d->cell_next;
977 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
984 if ( d->block != d->pgc->cell_playback[d->cell_cur].first_sector )
986 hb_log( "dvd: beginning of cell %d at block %d", d->cell_cur,
991 hb_error( "dvd: assuming missed end of cell %d", d->cell_cur );
992 d->cell_cur = d->cell_next;
993 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
1000 d->cur_vob_id = dsi_pack.dsi_gi.vobu_vob_idn;
1001 d->cur_cell_id = dsi_pack.dsi_gi.vobu_c_idn;
1003 if( d->cell_overlap )
1005 b->new_chap = hb_dvd_is_break( d );
1006 d->cell_overlap = 0;
1011 if( ( dsi_pack.vobu_sri.next_vobu & (1 << 31 ) ) == 0 ||
1012 ( dsi_pack.vobu_sri.next_vobu & 0x3fffffff ) == 0x3fffffff )
1014 if ( d->block <= d->pgc->cell_playback[d->cell_cur].first_sector ||
1015 d->block > d->pgc->cell_playback[d->cell_cur].last_sector )
1017 hb_log( "dvd: end of cell %d at block %d", d->cell_cur,
1020 d->cell_cur = d->cell_next;
1022 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
1024 d->cell_overlap = 1;
1030 if( DVDReadBlocks( d->file, d->block, 1, b->data ) != 1 )
1032 // this may be a real DVD error or may be DRM. Either way
1033 // we don't want to quit because of one bad block so set
1034 // things up so we'll advance to the next vobu and recurse.
1035 hb_error( "dvd: DVDReadBlocks failed (%d), skipping to vobu %u",
1036 d->block, d->next_vobu );
1038 goto top; /* XXX need to restructure this routine & avoid goto */
1048 /***********************************************************************
1050 ***********************************************************************
1051 * Returns in which chapter the next block to be read is.
1052 * Chapter numbers start at 1.
1053 **********************************************************************/
1054 int hb_dvd_chapter( hb_dvd_t * d )
1058 int nr_of_ptts = d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
1061 for( i = nr_of_ptts - 1;
1065 /* Get pgc for chapter (i+1) */
1066 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
1067 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
1068 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1070 if( d->cell_cur - d->cell_overlap >= pgc->program_map[pgn-1] - 1 &&
1071 d->cell_cur - d->cell_overlap <= pgc->nr_of_cells - 1 )
1073 /* We are in this chapter */
1082 /***********************************************************************
1084 ***********************************************************************
1085 * Returns chapter number if the current block is a new chapter start
1086 **********************************************************************/
1087 int hb_dvd_is_break( hb_dvd_t * d )
1091 int nr_of_ptts = d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
1095 for( i = nr_of_ptts - 1;
1099 /* Get pgc for chapter (i+1) */
1100 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
1101 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
1102 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1103 cell = pgc->program_map[pgn-1] - 1;
1105 if( cell <= d->cell_start )
1108 // This must not match against the start cell.
1109 if( pgc->cell_playback[cell].first_sector == d->block && cell != d->cell_start )
1118 /***********************************************************************
1120 ***********************************************************************
1121 * Closes and frees everything
1122 **********************************************************************/
1123 void hb_dvd_close( hb_dvd_t ** _d )
1133 DVDClose( d->reader );
1140 /***********************************************************************
1142 ***********************************************************************
1143 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1144 * cell to be read when we will be done with cell_cur.
1145 **********************************************************************/
1146 static void FindNextCell( hb_dvd_t * d )
1150 if( d->pgc->cell_playback[d->cell_cur].block_type ==
1151 BLOCK_TYPE_ANGLE_BLOCK )
1154 while( d->pgc->cell_playback[d->cell_cur+i].block_mode !=
1155 BLOCK_MODE_LAST_CELL )
1159 d->cell_next = d->cell_cur + i + 1;
1160 hb_log( "dvd: Skipping multi-angle cells %d-%d",
1166 d->cell_next = d->cell_cur + 1;
1170 /***********************************************************************
1172 ***********************************************************************
1174 **********************************************************************/
1175 static int dvdtime2msec(dvd_time_t * dt)
1177 double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
1178 double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
1180 ms = (((dt->hour & 0xf0) >> 3) * 5 + (dt->hour & 0x0f)) * 3600000;
1181 ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
1182 ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
1186 ms += ((dt->frame_u & 0x30) >> 3) * 5 +
1187 (dt->frame_u & 0x0f) * 1000.0 / fps;