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 ) ) )
86 hb_error( "dvd: DVDOpen failed (%s)", path );
91 if( !( d->vmg = ifoOpen( d->reader, 0 ) ) )
93 hb_error( "dvd: ifoOpen failed" );
97 d->path = strdup( path );
102 if( d->vmg ) ifoClose( d->vmg );
103 if( d->reader ) DVDClose( d->reader );
108 /***********************************************************************
110 **********************************************************************/
111 int hb_dvd_title_count( hb_dvd_t * d )
113 return d->vmg->tt_srpt->nr_of_srpts;
116 /***********************************************************************
118 **********************************************************************/
119 hb_title_t * hb_dvd_title_scan( hb_dvd_t * d, int t )
123 ifo_handle_t * vts = NULL;
125 hb_chapter_t * chapter;
128 float duration_correction;
129 unsigned char unused[1024];
131 hb_log( "scan: scanning title %d", t );
133 title = hb_title_init( d->path, t );
134 if( DVDUDFVolumeInfo( d->reader, title->name, sizeof( title->name ),
135 unused, sizeof( unused ) ) )
137 char * p_cur, * p_last = d->path;
138 for( p_cur = d->path; *p_cur; p_cur++ )
140 if( p_cur[0] == '/' && p_cur[1] )
145 snprintf( title->name, sizeof( title->name ), "%s", p_last );
148 /* VTS which our title is in */
149 title->vts = d->vmg->tt_srpt->title[t-1].title_set_nr;
153 /* A VTS of 0 means the title wasn't found in the title set */
154 hb_error("Invalid VTS (title set) number: %i", title->vts);
158 hb_log( "scan: opening IFO for VTS %d", title->vts );
159 if( !( vts = ifoOpen( d->reader, title->vts ) ) )
161 hb_error( "scan: ifoOpen failed" );
165 /* Position of the title in the VTS */
166 title->ttn = d->vmg->tt_srpt->title[t-1].vts_ttn;
169 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
170 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
171 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
173 hb_log("pgc_id: %d, pgn: %d: pgc: 0x%x", pgc_id, pgn, d->pgc);
177 hb_error( "scan: pgc not valid, skipping" );
181 if( pgn <= 0 || pgn > 99 )
183 hb_error( "scan: pgn %d not valid, skipping", pgn );
188 title->cell_start = d->pgc->program_map[pgn-1] - 1;
189 title->block_start = d->pgc->cell_playback[title->cell_start].first_sector;
192 title->cell_end = d->pgc->nr_of_cells - 1;
193 title->block_end = d->pgc->cell_playback[title->cell_end].last_sector;
196 title->block_count = 0;
197 d->cell_cur = title->cell_start;
198 while( d->cell_cur <= title->cell_end )
200 #define cp d->pgc->cell_playback[d->cell_cur]
201 title->block_count += cp.last_sector + 1 - cp.first_sector;
204 d->cell_cur = d->cell_next;
207 hb_log( "scan: vts=%d, ttn=%d, cells=%d->%d, blocks=%d->%d, "
208 "%d blocks", title->vts, title->ttn, title->cell_start,
209 title->cell_end, title->block_start, title->block_end,
210 title->block_count );
213 title->duration = 90LL * dvdtime2msec( &d->pgc->playback_time );
214 title->hours = title->duration / 90000 / 3600;
215 title->minutes = ( ( title->duration / 90000 ) % 3600 ) / 60;
216 title->seconds = ( title->duration / 90000 ) % 60;
217 hb_log( "scan: duration is %02d:%02d:%02d (%lld ms)",
218 title->hours, title->minutes, title->seconds,
219 title->duration / 90 );
221 /* ignore titles under 10 seconds because they're often stills or
222 * clips with no audio & our preview code doesn't currently handle
223 * either of these. */
224 if( title->duration < 900000LL )
226 hb_log( "scan: ignoring title (too short)" );
230 /* Detect languages */
231 for( i = 0; i < vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
233 hb_audio_t * audio, * audio_tmp;
234 int audio_format, lang_code, audio_control,
236 iso639_lang_t * lang;
238 hb_log( "scan: checking audio %d", i + 1 );
240 audio = calloc( sizeof( hb_audio_t ), 1 );
242 audio_format = vts->vtsi_mat->vts_audio_attr[i].audio_format;
243 lang_code = vts->vtsi_mat->vts_audio_attr[i].lang_code;
245 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i];
247 if( !( audio_control & 0x8000 ) )
249 hb_log( "scan: audio channel is not active" );
254 position = ( audio_control & 0x7F00 ) >> 8;
256 switch( audio_format )
259 audio->id = ( ( 0x80 + position ) << 8 ) | 0xbd;
260 audio->config.in.codec = HB_ACODEC_AC3;
265 audio->id = 0xc0 + position;
266 audio->config.in.codec = HB_ACODEC_MPGA;
270 audio->id = ( ( 0xa0 + position ) << 8 ) | 0xbd;
271 audio->config.in.codec = HB_ACODEC_LPCM;
275 audio->id = ( ( 0x88 + position ) << 8 ) | 0xbd;
276 audio->config.in.codec = HB_ACODEC_DCA;
281 audio->config.in.codec = 0;
282 hb_log( "scan: unknown audio codec (%x)",
291 /* Check for duplicate tracks */
293 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
295 audio_tmp = hb_list_item( title->list_audio, j );
296 if( audio->id == audio_tmp->id )
304 hb_log( "scan: duplicate audio track" );
309 lang = lang_for_code( vts->vtsi_mat->vts_audio_attr[i].lang_code );
311 snprintf( audio->config.lang.description, sizeof( audio->config.lang.description ), "%s (%s)",
312 strlen(lang->native_name) ? lang->native_name : lang->eng_name,
313 audio->config.in.codec == HB_ACODEC_AC3 ? "AC3" : ( audio->config.in.codec ==
314 HB_ACODEC_DCA ? "DTS" : ( audio->config.in.codec ==
315 HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) ) );
316 snprintf( audio->config.lang.simple, sizeof( audio->config.lang.simple ), "%s",
317 strlen(lang->native_name) ? lang->native_name : lang->eng_name );
318 snprintf( audio->config.lang.iso639_2, sizeof( audio->config.lang.iso639_2 ), "%s",
321 hb_log( "scan: id=%x, lang=%s, 3cc=%s", audio->id,
322 audio->config.lang.description, audio->config.lang.iso639_2 );
324 audio->config.in.track = i;
325 hb_list_add( title->list_audio, audio );
328 if( !hb_list_count( title->list_audio ) )
330 hb_log( "scan: ignoring title (no audio track)" );
334 memcpy( title->palette,
335 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette,
336 16 * sizeof( uint32_t ) );
338 /* Check for subtitles */
339 for( i = 0; i < vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
341 hb_subtitle_t * subtitle;
344 iso639_lang_t * lang;
346 hb_log( "scan: checking subtitle %d", i + 1 );
349 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i];
351 if( !( spu_control & 0x80000000 ) )
353 hb_log( "scan: subtitle channel is not active" );
357 if( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
359 switch( vts->vtsi_mat->vts_video_attr.permitted_df )
362 position = spu_control & 0xFF;
365 position = ( spu_control >> 8 ) & 0xFF;
368 position = ( spu_control >> 16 ) & 0xFF;
373 position = ( spu_control >> 24 ) & 0x7F;
376 lang = lang_for_code( vts->vtsi_mat->vts_subp_attr[i].lang_code );
378 subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
379 subtitle->id = ( ( 0x20 + position ) << 8 ) | 0xbd;
380 snprintf( subtitle->lang, sizeof( subtitle->lang ), "%s",
381 strlen(lang->native_name) ? lang->native_name : lang->eng_name);
382 snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ), "%s",
385 hb_log( "scan: id=%x, lang=%s, 3cc=%s", subtitle->id,
386 subtitle->lang, subtitle->iso639_2 );
388 hb_list_add( title->list_subtitle, subtitle );
392 hb_log( "scan: title %d has %d chapters", t,
393 vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts );
395 i < vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts; i++ )
397 int pgc_id_next, pgn_next;
400 chapter = calloc( sizeof( hb_chapter_t ), 1 );
401 /* remember the on-disc chapter number */
402 chapter->index = i + 1;
404 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgcn;
405 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgn;
406 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
409 chapter->cell_start = d->pgc->program_map[pgn-1] - 1;
410 chapter->block_start =
411 d->pgc->cell_playback[chapter->cell_start].first_sector;
414 if( i != vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts - 1 )
416 /* The cell before the starting cell of the next chapter,
418 pgc_id_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgcn;
419 pgn_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgn;
420 pgc_next = vts->vts_pgcit->pgci_srp[pgc_id_next-1].pgc;
421 chapter->cell_end = pgc_next->program_map[pgn_next-1] - 2;
422 if( chapter->cell_end < 0 )
431 /* ... the last cell of the title */
432 chapter->cell_end = title->cell_end;
435 d->pgc->cell_playback[chapter->cell_end].last_sector;
437 /* Block count, duration */
438 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
439 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
440 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
441 chapter->block_count = 0;
442 chapter->duration = 0;
444 d->cell_cur = chapter->cell_start;
445 while( d->cell_cur <= chapter->cell_end )
447 #define cp d->pgc->cell_playback[d->cell_cur]
448 chapter->block_count += cp.last_sector + 1 - cp.first_sector;
449 chapter->duration += 90LL * dvdtime2msec( &cp.playback_time );
452 d->cell_cur = d->cell_next;
454 hb_list_add( title->list_chapter, chapter );
458 /* The durations we get for chapters aren't precise. Scale them so
459 the total matches the title duration */
461 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
463 chapter = hb_list_item( title->list_chapter, i );
464 duration += chapter->duration;
466 duration_correction = (float) title->duration / (float) duration;
467 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
470 chapter = hb_list_item( title->list_chapter, i );
471 chapter->duration = duration_correction * chapter->duration;
472 seconds = ( chapter->duration + 45000 ) / 90000;
473 chapter->hours = seconds / 3600;
474 chapter->minutes = ( seconds % 3600 ) / 60;
475 chapter->seconds = seconds % 60;
477 hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %lld ms",
478 chapter->index, chapter->cell_start, chapter->cell_end,
479 chapter->block_start, chapter->block_end,
480 chapter->block_count, chapter->duration / 90 );
483 /* Get aspect. We don't get width/height/rate infos here as
484 they tend to be wrong */
485 switch( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
488 title->aspect = HB_ASPECT_BASE * 4 / 3;
491 title->aspect = HB_ASPECT_BASE * 16 / 9;
494 hb_log( "scan: unknown aspect" );
498 hb_log( "scan: aspect = %d", title->aspect );
500 /* This title is ok so far */
504 hb_list_close( &title->list_audio );
509 if( vts ) ifoClose( vts );
514 /***********************************************************************
516 ***********************************************************************
517 * Title and chapter start at 1
518 **********************************************************************/
519 int hb_dvd_start( hb_dvd_t * d, int title, int chapter )
524 /* Open the IFO and the VOBs for this title */
525 d->vts = d->vmg->tt_srpt->title[title-1].title_set_nr;
526 d->ttn = d->vmg->tt_srpt->title[title-1].vts_ttn;
527 if( !( d->ifo = ifoOpen( d->reader, d->vts ) ) )
529 hb_error( "dvd: ifoOpen failed for VTS %d", d->vts );
532 if( !( d->file = DVDOpenFile( d->reader, d->vts,
533 DVD_READ_TITLE_VOBS ) ) )
535 hb_error( "dvd: DVDOpenFile failed for VTS %d", d->vts );
539 /* Get title first and last blocks */
540 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgcn;
541 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgn;
542 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
543 d->cell_start = d->pgc->program_map[pgn - 1] - 1;
544 d->cell_end = d->pgc->nr_of_cells - 1;
545 d->title_start = d->pgc->cell_playback[d->cell_start].first_sector;
546 d->title_end = d->pgc->cell_playback[d->cell_end].last_sector;
549 d->title_block_count = 0;
550 for( i = d->cell_start; i <= d->cell_end; i++ )
552 d->title_block_count += d->pgc->cell_playback[i].last_sector + 1 -
553 d->pgc->cell_playback[i].first_sector;
556 /* Get pgc for the current chapter */
557 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgcn;
558 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgn;
559 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
561 /* Get the two first cells */
562 d->cell_cur = d->pgc->program_map[pgn-1] - 1;
565 d->block = d->pgc->cell_playback[d->cell_cur].first_sector;
566 d->next_vobu = d->block;
575 /***********************************************************************
577 ***********************************************************************
579 **********************************************************************/
580 void hb_dvd_stop( hb_dvd_t * d )
589 DVDCloseFile( d->file );
594 /***********************************************************************
596 ***********************************************************************
598 **********************************************************************/
599 int hb_dvd_seek( hb_dvd_t * d, float f )
604 count = f * d->title_block_count;
606 for( i = d->cell_start; i <= d->cell_end; i++ )
608 sizeCell = d->pgc->cell_playback[i].last_sector + 1 -
609 d->pgc->cell_playback[i].first_sector;
611 if( count < sizeCell )
617 /* Now let hb_dvd_read find the next VOBU */
618 d->next_vobu = d->pgc->cell_playback[i].first_sector + count;
626 if( i > d->cell_end )
632 * Assume that we are in sync, even if we are not given that it is obvious
633 * that we are out of sync if we have just done a seek.
641 /***********************************************************************
643 ***********************************************************************/
644 int is_nav_pack( unsigned char *buf )
647 * The NAV Pack is comprised of the PCI Packet and DSI Packet, both
648 * of these start at known offsets and start with a special identifier.
651 * PCI = { 00 00 01 bf # private stream header
656 * DSI = { 00 00 01 bf # private stream header
662 * The PCI starts at offset 0x26 into the sector, and the DSI starts at 0x400
664 * This information from: http://dvd.sourceforge.net/dvdinfo/
666 if( ( buf[0x26] == 0x00 && // PCI
670 buf[0x2c] == 0x00 ) &&
671 ( buf[0x400] == 0x00 && // DSI
672 buf[0x401] == 0x00 &&
673 buf[0x402] == 0x01 &&
674 buf[0x403] == 0xbf &&
675 buf[0x406] == 0x01 ) )
684 /***********************************************************************
686 ***********************************************************************
688 **********************************************************************/
689 int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
698 // if we've just finished the last cell of the title we don't
699 // want to read another block because our next_vobu pointer
700 // is probably invalid. Just return 'no data' & our caller
701 // should check and discover we're at eof.
702 if ( d->cell_cur > d->cell_end )
707 int block, pack_len, next_vobu, read_retry;
709 for( read_retry = 0; read_retry < 3; read_retry++ )
711 if( DVDReadBlocks( d->file, d->next_vobu, 1, b->data ) == 1 )
718 hb_log( "dvd: Read Error on blk %d, attempt %d",
719 d->next_vobu, read_retry );
723 if( read_retry == 3 )
725 hb_log( "dvd: vobu read error blk %d - skipping to cell %d",
726 d->next_vobu, d->cell_next );
727 d->cell_cur = d->cell_next;
728 if ( d->cell_cur > d->cell_end )
731 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
737 if ( !is_nav_pack( b->data ) ) {
739 if( d->in_sync == 1 ) {
740 hb_log("dvd: Lost sync, searching for NAV pack at blk %d",
747 navRead_DSI( &dsi_pack, &b->data[DSI_START_BYTE] );
749 if ( d->in_sync == 0 && d->cur_cell_id &&
750 (d->cur_vob_id != dsi_pack.dsi_gi.vobu_vob_idn ||
751 d->cur_cell_id != dsi_pack.dsi_gi.vobu_c_idn ) )
753 // We walked out of the cell we're supposed to be in.
754 // If we're not at the start of our next cell go there.
755 hb_log("dvd: left cell %d (%u,%u) for (%u,%u) at block %u",
756 d->cell_cur, d->cur_vob_id, d->cur_cell_id,
757 dsi_pack.dsi_gi.vobu_vob_idn, dsi_pack.dsi_gi.vobu_c_idn,
759 if ( d->next_vobu != d->pgc->cell_playback[d->cell_next].first_sector )
761 d->next_vobu = d->pgc->cell_playback[d->cell_next].first_sector;
767 block = dsi_pack.dsi_gi.nv_pck_lbn;
768 pack_len = dsi_pack.dsi_gi.vobu_ea;
770 // There are a total of 21 next vobu offsets (and 21 prev_vobu
771 // offsets) in the navpack SRI structure. The primary one is
772 // 'next_vobu' which is the offset (in dvd blocks) from the current
773 // block to the start of the next vobu. If the block at 'next_vobu'
774 // can't be read, 'next_video' is the offset to the vobu closest to it.
775 // The other 19 offsets are vobus at successively longer distances from
776 // the current block (this is so that a hardware player can do
777 // adaptive error recovery to skip over a bad spot on the disk). In all
778 // these offsets the high bit is set to indicate when it contains a
779 // valid offset. The next bit (2^30) is set to indicate that there's
780 // another valid offset in the SRI that's closer to the current block.
781 // A hardware player uses these bits to chose the closest valid offset
782 // and uses that as its next vobu. (Some mastering schemes appear to
783 // put a bogus offset in next_vobu with the 2^30 bit set & the
784 // correct offset in next_video. This works fine in hardware players
785 // but will mess up software that doesn't implement the full next
786 // vobu decision logic.) In addition to the flag bits there's a
787 // reserved value of the offset that indicates 'no next vobu' (which
788 // indicates the end of a cell). But having no next vobu pointer with a
789 // 'valid' bit set also indicates end of cell. Different mastering
790 // schemes seem to use all possible combinations of the flag bits
791 // and reserved values to indicate end of cell so we have to check
792 // them all or we'll get a disk read error from following an
794 uint32_t next_ptr = dsi_pack.vobu_sri.next_vobu;
795 if ( ( next_ptr & ( 1 << 31 ) ) == 0 ||
796 ( next_ptr & ( 1 << 30 ) ) != 0 ||
797 ( next_ptr & 0x3fffffff ) == 0x3fffffff )
799 next_ptr = dsi_pack.vobu_sri.next_video;
800 if ( ( next_ptr & ( 1 << 31 ) ) == 0 ||
801 ( next_ptr & 0x3fffffff ) == 0x3fffffff )
803 // don't have a valid forward pointer - assume end-of-cell
805 d->pack_len = pack_len;
809 next_vobu = block + ( next_ptr & 0x3fffffff );
813 block >= d->next_vobu &&
814 ( block <= d->title_start + d->title_block_count ||
815 block <= d->title_end ) )
818 d->pack_len = pack_len;
819 d->next_vobu = next_vobu;
823 /* Wasn't a valid VOBU, try next block */
826 hb_log( "dvd: couldn't find a VOBU after 1024 blocks" );
833 if( d->in_sync == 0 || d->in_sync == 2 )
835 if( d->in_sync == 0 )
837 hb_log( "dvd: In sync with DVD at block %d", d->block );
842 // Revert the cell overlap, and check for a chapter break
843 // If this cell is zero length (prev_vobu & next_vobu both
844 // set to END_OF_CELL) we need to check for beginning of
845 // cell before checking for end or we'll advance to the next
846 // cell too early and fail to generate a chapter mark when this
847 // cell starts a chapter.
848 if( ( dsi_pack.vobu_sri.prev_vobu & (1 << 31 ) ) == 0 ||
849 ( dsi_pack.vobu_sri.prev_vobu & 0x3fffffff ) == 0x3fffffff )
851 // A vobu that's not at the start of a cell can have an
852 // EOC prev pointer (this seems to be associated with some
853 // sort of drm). The rest of the content in the cell may be
854 // booby-trapped so treat this like an end-of-cell rather
855 // than a beginning of cell.
856 if ( d->pgc->cell_playback[d->cell_cur].first_sector < dsi_pack.dsi_gi.nv_pck_lbn &&
857 d->pgc->cell_playback[d->cell_cur].last_sector >= dsi_pack.dsi_gi.nv_pck_lbn )
859 hb_log( "dvd: null prev_vobu in cell %d at block %d", d->cell_cur,
861 // treat like end-of-cell then go directly to start of next cell.
862 d->cell_cur = d->cell_next;
864 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
871 if ( d->block != d->pgc->cell_playback[d->cell_cur].first_sector )
873 hb_log( "dvd: beginning of cell %d at block %d", d->cell_cur,
878 hb_error( "dvd: assuming missed end of cell %d", d->cell_cur );
879 d->cell_cur = d->cell_next;
880 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
887 d->cur_vob_id = dsi_pack.dsi_gi.vobu_vob_idn;
888 d->cur_cell_id = dsi_pack.dsi_gi.vobu_c_idn;
890 if( d->cell_overlap )
892 b->new_chap = hb_dvd_is_break( d );
898 if( ( dsi_pack.vobu_sri.next_vobu & (1 << 31 ) ) == 0 ||
899 ( dsi_pack.vobu_sri.next_vobu & 0x3fffffff ) == 0x3fffffff )
901 if ( d->block <= d->pgc->cell_playback[d->cell_cur].first_sector ||
902 d->block > d->pgc->cell_playback[d->cell_cur].last_sector )
904 hb_log( "dvd: end of cell %d at block %d", d->cell_cur,
907 d->cell_cur = d->cell_next;
909 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
917 if( DVDReadBlocks( d->file, d->block, 1, b->data ) != 1 )
919 // this may be a real DVD error or may be DRM. Either way
920 // we don't want to quit because of one bad block so set
921 // things up so we'll advance to the next vobu and recurse.
922 hb_error( "dvd: DVDReadBlocks failed (%d), skipping to vobu %u",
923 d->block, d->next_vobu );
925 goto top; /* XXX need to restructure this routine & avoid goto */
935 /***********************************************************************
937 ***********************************************************************
938 * Returns in which chapter the next block to be read is.
939 * Chapter numbers start at 1.
940 **********************************************************************/
941 int hb_dvd_chapter( hb_dvd_t * d )
945 int nr_of_ptts = d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
948 for( i = nr_of_ptts - 1;
952 /* Get pgc for chapter (i+1) */
953 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
954 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
955 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
957 if( d->cell_cur - d->cell_overlap >= pgc->program_map[pgn-1] - 1 &&
958 d->cell_cur - d->cell_overlap <= pgc->nr_of_cells - 1 )
960 /* We are in this chapter */
969 /***********************************************************************
971 ***********************************************************************
972 * Returns chapter number if the current block is a new chapter start
973 **********************************************************************/
974 int hb_dvd_is_break( hb_dvd_t * d )
978 int nr_of_ptts = d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
982 for( i = nr_of_ptts - 1;
986 /* Get pgc for chapter (i+1) */
987 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
988 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
989 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
990 cell = pgc->program_map[pgn-1] - 1;
992 if( cell <= d->cell_start )
995 // This must not match against the start cell.
996 if( pgc->cell_playback[cell].first_sector == d->block && cell != d->cell_start )
1005 /***********************************************************************
1007 ***********************************************************************
1008 * Closes and frees everything
1009 **********************************************************************/
1010 void hb_dvd_close( hb_dvd_t ** _d )
1020 DVDClose( d->reader );
1027 /***********************************************************************
1029 ***********************************************************************
1030 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1031 * cell to be read when we will be done with cell_cur.
1032 **********************************************************************/
1033 static void FindNextCell( hb_dvd_t * d )
1037 if( d->pgc->cell_playback[d->cell_cur].block_type ==
1038 BLOCK_TYPE_ANGLE_BLOCK )
1041 while( d->pgc->cell_playback[d->cell_cur+i].block_mode !=
1042 BLOCK_MODE_LAST_CELL )
1046 d->cell_next = d->cell_cur + i + 1;
1047 hb_log( "dvd: Skipping multi-angle cells %d-%d",
1053 d->cell_next = d->cell_cur + 1;
1057 /***********************************************************************
1059 ***********************************************************************
1061 **********************************************************************/
1062 static int dvdtime2msec(dvd_time_t * dt)
1064 double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
1065 double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
1067 ms = (((dt->hour & 0xf0) >> 3) * 5 + (dt->hour & 0x0f)) * 3600000;
1068 ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
1069 ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
1073 ms += ((dt->frame_u & 0x30) >> 3) * 5 +
1074 (dt->frame_u & 0x0f) * 1000.0 / fps;