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 /* Position of the title in the VTS */
169 title->ttn = d->vmg->tt_srpt->title[t-1].vts_ttn;
172 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
173 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
174 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
176 hb_log("pgc_id: %d, pgn: %d: pgc: 0x%x", pgc_id, pgn, d->pgc);
180 hb_error( "scan: pgc not valid, skipping" );
184 if( pgn <= 0 || pgn > 99 )
186 hb_error( "scan: pgn %d not valid, skipping", pgn );
191 title->cell_start = d->pgc->program_map[pgn-1] - 1;
192 title->block_start = d->pgc->cell_playback[title->cell_start].first_sector;
195 title->cell_end = d->pgc->nr_of_cells - 1;
196 title->block_end = d->pgc->cell_playback[title->cell_end].last_sector;
199 title->block_count = 0;
200 d->cell_cur = title->cell_start;
201 while( d->cell_cur <= title->cell_end )
203 #define cp d->pgc->cell_playback[d->cell_cur]
204 title->block_count += cp.last_sector + 1 - cp.first_sector;
207 d->cell_cur = d->cell_next;
210 hb_log( "scan: vts=%d, ttn=%d, cells=%d->%d, blocks=%d->%d, "
211 "%d blocks", title->vts, title->ttn, title->cell_start,
212 title->cell_end, title->block_start, title->block_end,
213 title->block_count );
216 title->duration = 90LL * dvdtime2msec( &d->pgc->playback_time );
217 title->hours = title->duration / 90000 / 3600;
218 title->minutes = ( ( title->duration / 90000 ) % 3600 ) / 60;
219 title->seconds = ( title->duration / 90000 ) % 60;
220 hb_log( "scan: duration is %02d:%02d:%02d (%lld ms)",
221 title->hours, title->minutes, title->seconds,
222 title->duration / 90 );
224 /* ignore titles under 10 seconds because they're often stills or
225 * clips with no audio & our preview code doesn't currently handle
226 * either of these. */
227 if( title->duration < 900000LL )
229 hb_log( "scan: ignoring title (too short)" );
233 /* Detect languages */
234 for( i = 0; i < vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
236 hb_audio_t * audio, * audio_tmp;
237 int audio_format, lang_code, audio_control,
239 iso639_lang_t * lang;
241 hb_log( "scan: checking audio %d", i + 1 );
243 audio = calloc( sizeof( hb_audio_t ), 1 );
245 audio_format = vts->vtsi_mat->vts_audio_attr[i].audio_format;
246 lang_code = vts->vtsi_mat->vts_audio_attr[i].lang_code;
248 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i];
250 if( !( audio_control & 0x8000 ) )
252 hb_log( "scan: audio channel is not active" );
257 position = ( audio_control & 0x7F00 ) >> 8;
259 switch( audio_format )
262 audio->id = ( ( 0x80 + position ) << 8 ) | 0xbd;
263 audio->config.in.codec = HB_ACODEC_AC3;
268 audio->id = 0xc0 + position;
269 audio->config.in.codec = HB_ACODEC_MPGA;
273 audio->id = ( ( 0xa0 + position ) << 8 ) | 0xbd;
274 audio->config.in.codec = HB_ACODEC_LPCM;
278 audio->id = ( ( 0x88 + position ) << 8 ) | 0xbd;
279 audio->config.in.codec = HB_ACODEC_DCA;
284 audio->config.in.codec = 0;
285 hb_log( "scan: unknown audio codec (%x)",
294 /* Check for duplicate tracks */
296 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
298 audio_tmp = hb_list_item( title->list_audio, j );
299 if( audio->id == audio_tmp->id )
307 hb_log( "scan: duplicate audio track" );
312 lang = lang_for_code( vts->vtsi_mat->vts_audio_attr[i].lang_code );
314 snprintf( audio->config.lang.description, sizeof( audio->config.lang.description ), "%s (%s)",
315 strlen(lang->native_name) ? lang->native_name : lang->eng_name,
316 audio->config.in.codec == HB_ACODEC_AC3 ? "AC3" : ( audio->config.in.codec ==
317 HB_ACODEC_DCA ? "DTS" : ( audio->config.in.codec ==
318 HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) ) );
319 snprintf( audio->config.lang.simple, sizeof( audio->config.lang.simple ), "%s",
320 strlen(lang->native_name) ? lang->native_name : lang->eng_name );
321 snprintf( audio->config.lang.iso639_2, sizeof( audio->config.lang.iso639_2 ), "%s",
324 hb_log( "scan: id=%x, lang=%s, 3cc=%s", audio->id,
325 audio->config.lang.description, audio->config.lang.iso639_2 );
327 audio->config.in.track = i;
328 hb_list_add( title->list_audio, audio );
331 if( !hb_list_count( title->list_audio ) )
333 hb_log( "scan: ignoring title (no audio track)" );
337 memcpy( title->palette,
338 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette,
339 16 * sizeof( uint32_t ) );
341 /* Check for subtitles */
342 for( i = 0; i < vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
344 hb_subtitle_t * subtitle;
347 iso639_lang_t * lang;
349 hb_log( "scan: checking subtitle %d", i + 1 );
352 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i];
354 if( !( spu_control & 0x80000000 ) )
356 hb_log( "scan: subtitle channel is not active" );
360 if( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
362 switch( vts->vtsi_mat->vts_video_attr.permitted_df )
365 position = spu_control & 0xFF;
368 position = ( spu_control >> 8 ) & 0xFF;
371 position = ( spu_control >> 16 ) & 0xFF;
376 position = ( spu_control >> 24 ) & 0x7F;
379 lang = lang_for_code( vts->vtsi_mat->vts_subp_attr[i].lang_code );
381 subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
382 subtitle->id = ( ( 0x20 + position ) << 8 ) | 0xbd;
383 snprintf( subtitle->lang, sizeof( subtitle->lang ), "%s",
384 strlen(lang->native_name) ? lang->native_name : lang->eng_name);
385 snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ), "%s",
388 hb_log( "scan: id=%x, lang=%s, 3cc=%s", subtitle->id,
389 subtitle->lang, subtitle->iso639_2 );
391 hb_list_add( title->list_subtitle, subtitle );
395 hb_log( "scan: title %d has %d chapters", t,
396 vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts );
398 i < vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts; i++ )
400 int pgc_id_next, pgn_next;
403 chapter = calloc( sizeof( hb_chapter_t ), 1 );
404 /* remember the on-disc chapter number */
405 chapter->index = i + 1;
407 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgcn;
408 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgn;
409 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
412 chapter->cell_start = d->pgc->program_map[pgn-1] - 1;
413 chapter->block_start =
414 d->pgc->cell_playback[chapter->cell_start].first_sector;
417 if( i != vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts - 1 )
419 /* The cell before the starting cell of the next chapter,
421 pgc_id_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgcn;
422 pgn_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgn;
423 pgc_next = vts->vts_pgcit->pgci_srp[pgc_id_next-1].pgc;
424 chapter->cell_end = pgc_next->program_map[pgn_next-1] - 2;
425 if( chapter->cell_end < 0 )
434 /* ... the last cell of the title */
435 chapter->cell_end = title->cell_end;
438 d->pgc->cell_playback[chapter->cell_end].last_sector;
440 /* Block count, duration */
441 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
442 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
443 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
444 chapter->block_count = 0;
445 chapter->duration = 0;
447 d->cell_cur = chapter->cell_start;
448 while( d->cell_cur <= chapter->cell_end )
450 #define cp d->pgc->cell_playback[d->cell_cur]
451 chapter->block_count += cp.last_sector + 1 - cp.first_sector;
452 chapter->duration += 90LL * dvdtime2msec( &cp.playback_time );
455 d->cell_cur = d->cell_next;
457 hb_list_add( title->list_chapter, chapter );
461 /* The durations we get for chapters aren't precise. Scale them so
462 the total matches the title duration */
464 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
466 chapter = hb_list_item( title->list_chapter, i );
467 duration += chapter->duration;
469 duration_correction = (float) title->duration / (float) duration;
470 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
473 chapter = hb_list_item( title->list_chapter, i );
474 chapter->duration = duration_correction * chapter->duration;
475 seconds = ( chapter->duration + 45000 ) / 90000;
476 chapter->hours = seconds / 3600;
477 chapter->minutes = ( seconds % 3600 ) / 60;
478 chapter->seconds = seconds % 60;
480 hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %lld ms",
481 chapter->index, chapter->cell_start, chapter->cell_end,
482 chapter->block_start, chapter->block_end,
483 chapter->block_count, chapter->duration / 90 );
486 /* Get aspect. We don't get width/height/rate infos here as
487 they tend to be wrong */
488 switch( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
491 title->container_aspect = 4. / 3.;
494 title->container_aspect = 16. / 9.;
497 hb_log( "scan: unknown aspect" );
501 hb_log( "scan: aspect = %g", title->aspect );
503 /* This title is ok so far */
507 hb_list_close( &title->list_audio );
512 if( vts ) ifoClose( vts );
517 /***********************************************************************
519 ***********************************************************************
520 * Title and chapter start at 1
521 **********************************************************************/
522 int hb_dvd_start( hb_dvd_t * d, int title, int chapter )
527 /* Open the IFO and the VOBs for this title */
528 d->vts = d->vmg->tt_srpt->title[title-1].title_set_nr;
529 d->ttn = d->vmg->tt_srpt->title[title-1].vts_ttn;
530 if( !( d->ifo = ifoOpen( d->reader, d->vts ) ) )
532 hb_error( "dvd: ifoOpen failed for VTS %d", d->vts );
535 if( !( d->file = DVDOpenFile( d->reader, d->vts,
536 DVD_READ_TITLE_VOBS ) ) )
538 hb_error( "dvd: DVDOpenFile failed for VTS %d", d->vts );
542 /* Get title first and last blocks */
543 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgcn;
544 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgn;
545 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
546 d->cell_start = d->pgc->program_map[pgn - 1] - 1;
547 d->cell_end = d->pgc->nr_of_cells - 1;
548 d->title_start = d->pgc->cell_playback[d->cell_start].first_sector;
549 d->title_end = d->pgc->cell_playback[d->cell_end].last_sector;
552 d->title_block_count = 0;
553 for( i = d->cell_start; i <= d->cell_end; i++ )
555 d->title_block_count += d->pgc->cell_playback[i].last_sector + 1 -
556 d->pgc->cell_playback[i].first_sector;
559 /* Get pgc for the current chapter */
560 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgcn;
561 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgn;
562 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
564 /* Get the two first cells */
565 d->cell_cur = d->pgc->program_map[pgn-1] - 1;
568 d->block = d->pgc->cell_playback[d->cell_cur].first_sector;
569 d->next_vobu = d->block;
578 /***********************************************************************
580 ***********************************************************************
582 **********************************************************************/
583 void hb_dvd_stop( hb_dvd_t * d )
592 DVDCloseFile( d->file );
597 /***********************************************************************
599 ***********************************************************************
601 **********************************************************************/
602 int hb_dvd_seek( hb_dvd_t * d, float f )
607 count = f * d->title_block_count;
609 for( i = d->cell_start; i <= d->cell_end; i++ )
611 sizeCell = d->pgc->cell_playback[i].last_sector + 1 -
612 d->pgc->cell_playback[i].first_sector;
614 if( count < sizeCell )
620 /* Now let hb_dvd_read find the next VOBU */
621 d->next_vobu = d->pgc->cell_playback[i].first_sector + count;
629 if( i > d->cell_end )
635 * Assume that we are in sync, even if we are not given that it is obvious
636 * that we are out of sync if we have just done a seek.
644 /***********************************************************************
646 ***********************************************************************/
647 int is_nav_pack( unsigned char *buf )
650 * The NAV Pack is comprised of the PCI Packet and DSI Packet, both
651 * of these start at known offsets and start with a special identifier.
654 * PCI = { 00 00 01 bf # private stream header
659 * DSI = { 00 00 01 bf # private stream header
665 * The PCI starts at offset 0x26 into the sector, and the DSI starts at 0x400
667 * This information from: http://dvd.sourceforge.net/dvdinfo/
669 if( ( buf[0x26] == 0x00 && // PCI
673 buf[0x2c] == 0x00 ) &&
674 ( buf[0x400] == 0x00 && // DSI
675 buf[0x401] == 0x00 &&
676 buf[0x402] == 0x01 &&
677 buf[0x403] == 0xbf &&
678 buf[0x406] == 0x01 ) )
687 /***********************************************************************
689 ***********************************************************************
691 **********************************************************************/
692 int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
701 // if we've just finished the last cell of the title we don't
702 // want to read another block because our next_vobu pointer
703 // is probably invalid. Just return 'no data' & our caller
704 // should check and discover we're at eof.
705 if ( d->cell_cur > d->cell_end )
710 int block, pack_len, next_vobu, read_retry;
712 for( read_retry = 0; read_retry < 3; read_retry++ )
714 if( DVDReadBlocks( d->file, d->next_vobu, 1, b->data ) == 1 )
721 hb_log( "dvd: Read Error on blk %d, attempt %d",
722 d->next_vobu, read_retry );
726 if( read_retry == 3 )
728 hb_log( "dvd: vobu read error blk %d - skipping to cell %d",
729 d->next_vobu, d->cell_next );
730 d->cell_cur = d->cell_next;
731 if ( d->cell_cur > d->cell_end )
734 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
740 if ( !is_nav_pack( b->data ) ) {
742 if( d->in_sync == 1 ) {
743 hb_log("dvd: Lost sync, searching for NAV pack at blk %d",
750 navRead_DSI( &dsi_pack, &b->data[DSI_START_BYTE] );
752 if ( d->in_sync == 0 && d->cur_cell_id &&
753 (d->cur_vob_id != dsi_pack.dsi_gi.vobu_vob_idn ||
754 d->cur_cell_id != dsi_pack.dsi_gi.vobu_c_idn ) )
756 // We walked out of the cell we're supposed to be in.
757 // If we're not at the start of our next cell go there.
758 hb_log("dvd: left cell %d (%u,%u) for (%u,%u) at block %u",
759 d->cell_cur, d->cur_vob_id, d->cur_cell_id,
760 dsi_pack.dsi_gi.vobu_vob_idn, dsi_pack.dsi_gi.vobu_c_idn,
762 if ( d->next_vobu != d->pgc->cell_playback[d->cell_next].first_sector )
764 d->next_vobu = d->pgc->cell_playback[d->cell_next].first_sector;
770 block = dsi_pack.dsi_gi.nv_pck_lbn;
771 pack_len = dsi_pack.dsi_gi.vobu_ea;
773 // There are a total of 21 next vobu offsets (and 21 prev_vobu
774 // offsets) in the navpack SRI structure. The primary one is
775 // 'next_vobu' which is the offset (in dvd blocks) from the current
776 // block to the start of the next vobu. If the block at 'next_vobu'
777 // can't be read, 'next_video' is the offset to the vobu closest to it.
778 // The other 19 offsets are vobus at successively longer distances from
779 // the current block (this is so that a hardware player can do
780 // adaptive error recovery to skip over a bad spot on the disk). In all
781 // these offsets the high bit is set to indicate when it contains a
782 // valid offset. The next bit (2^30) is set to indicate that there's
783 // another valid offset in the SRI that's closer to the current block.
784 // A hardware player uses these bits to chose the closest valid offset
785 // and uses that as its next vobu. (Some mastering schemes appear to
786 // put a bogus offset in next_vobu with the 2^30 bit set & the
787 // correct offset in next_video. This works fine in hardware players
788 // but will mess up software that doesn't implement the full next
789 // vobu decision logic.) In addition to the flag bits there's a
790 // reserved value of the offset that indicates 'no next vobu' (which
791 // indicates the end of a cell). But having no next vobu pointer with a
792 // 'valid' bit set also indicates end of cell. Different mastering
793 // schemes seem to use all possible combinations of the flag bits
794 // and reserved values to indicate end of cell so we have to check
795 // them all or we'll get a disk read error from following an
797 uint32_t next_ptr = dsi_pack.vobu_sri.next_vobu;
798 if ( ( next_ptr & ( 1 << 31 ) ) == 0 ||
799 ( next_ptr & ( 1 << 30 ) ) != 0 ||
800 ( next_ptr & 0x3fffffff ) == 0x3fffffff )
802 next_ptr = dsi_pack.vobu_sri.next_video;
803 if ( ( next_ptr & ( 1 << 31 ) ) == 0 ||
804 ( next_ptr & 0x3fffffff ) == 0x3fffffff )
806 // don't have a valid forward pointer - assume end-of-cell
808 d->pack_len = pack_len;
812 next_vobu = block + ( next_ptr & 0x3fffffff );
816 block >= d->next_vobu &&
817 ( block <= d->title_start + d->title_block_count ||
818 block <= d->title_end ) )
821 d->pack_len = pack_len;
822 d->next_vobu = next_vobu;
826 /* Wasn't a valid VOBU, try next block */
829 hb_log( "dvd: couldn't find a VOBU after 1024 blocks" );
836 if( d->in_sync == 0 || d->in_sync == 2 )
838 if( d->in_sync == 0 )
840 hb_log( "dvd: In sync with DVD at block %d", d->block );
845 // Revert the cell overlap, and check for a chapter break
846 // If this cell is zero length (prev_vobu & next_vobu both
847 // set to END_OF_CELL) we need to check for beginning of
848 // cell before checking for end or we'll advance to the next
849 // cell too early and fail to generate a chapter mark when this
850 // cell starts a chapter.
851 if( ( dsi_pack.vobu_sri.prev_vobu & (1 << 31 ) ) == 0 ||
852 ( dsi_pack.vobu_sri.prev_vobu & 0x3fffffff ) == 0x3fffffff )
854 // A vobu that's not at the start of a cell can have an
855 // EOC prev pointer (this seems to be associated with some
856 // sort of drm). The rest of the content in the cell may be
857 // booby-trapped so treat this like an end-of-cell rather
858 // than a beginning of cell.
859 if ( d->pgc->cell_playback[d->cell_cur].first_sector < dsi_pack.dsi_gi.nv_pck_lbn &&
860 d->pgc->cell_playback[d->cell_cur].last_sector >= dsi_pack.dsi_gi.nv_pck_lbn )
862 hb_log( "dvd: null prev_vobu in cell %d at block %d", d->cell_cur,
864 // treat like end-of-cell then go directly to start of next cell.
865 d->cell_cur = d->cell_next;
867 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
874 if ( d->block != d->pgc->cell_playback[d->cell_cur].first_sector )
876 hb_log( "dvd: beginning of cell %d at block %d", d->cell_cur,
881 hb_error( "dvd: assuming missed end of cell %d", d->cell_cur );
882 d->cell_cur = d->cell_next;
883 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
890 d->cur_vob_id = dsi_pack.dsi_gi.vobu_vob_idn;
891 d->cur_cell_id = dsi_pack.dsi_gi.vobu_c_idn;
893 if( d->cell_overlap )
895 b->new_chap = hb_dvd_is_break( d );
901 if( ( dsi_pack.vobu_sri.next_vobu & (1 << 31 ) ) == 0 ||
902 ( dsi_pack.vobu_sri.next_vobu & 0x3fffffff ) == 0x3fffffff )
904 if ( d->block <= d->pgc->cell_playback[d->cell_cur].first_sector ||
905 d->block > d->pgc->cell_playback[d->cell_cur].last_sector )
907 hb_log( "dvd: end of cell %d at block %d", d->cell_cur,
910 d->cell_cur = d->cell_next;
912 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
920 if( DVDReadBlocks( d->file, d->block, 1, b->data ) != 1 )
922 // this may be a real DVD error or may be DRM. Either way
923 // we don't want to quit because of one bad block so set
924 // things up so we'll advance to the next vobu and recurse.
925 hb_error( "dvd: DVDReadBlocks failed (%d), skipping to vobu %u",
926 d->block, d->next_vobu );
928 goto top; /* XXX need to restructure this routine & avoid goto */
938 /***********************************************************************
940 ***********************************************************************
941 * Returns in which chapter the next block to be read is.
942 * Chapter numbers start at 1.
943 **********************************************************************/
944 int hb_dvd_chapter( hb_dvd_t * d )
948 int nr_of_ptts = d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
951 for( i = nr_of_ptts - 1;
955 /* Get pgc for chapter (i+1) */
956 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
957 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
958 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
960 if( d->cell_cur - d->cell_overlap >= pgc->program_map[pgn-1] - 1 &&
961 d->cell_cur - d->cell_overlap <= pgc->nr_of_cells - 1 )
963 /* We are in this chapter */
972 /***********************************************************************
974 ***********************************************************************
975 * Returns chapter number if the current block is a new chapter start
976 **********************************************************************/
977 int hb_dvd_is_break( hb_dvd_t * d )
981 int nr_of_ptts = d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
985 for( i = nr_of_ptts - 1;
989 /* Get pgc for chapter (i+1) */
990 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
991 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
992 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
993 cell = pgc->program_map[pgn-1] - 1;
995 if( cell <= d->cell_start )
998 // This must not match against the start cell.
999 if( pgc->cell_playback[cell].first_sector == d->block && cell != d->cell_start )
1008 /***********************************************************************
1010 ***********************************************************************
1011 * Closes and frees everything
1012 **********************************************************************/
1013 void hb_dvd_close( hb_dvd_t ** _d )
1023 DVDClose( d->reader );
1030 /***********************************************************************
1032 ***********************************************************************
1033 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1034 * cell to be read when we will be done with cell_cur.
1035 **********************************************************************/
1036 static void FindNextCell( hb_dvd_t * d )
1040 if( d->pgc->cell_playback[d->cell_cur].block_type ==
1041 BLOCK_TYPE_ANGLE_BLOCK )
1044 while( d->pgc->cell_playback[d->cell_cur+i].block_mode !=
1045 BLOCK_MODE_LAST_CELL )
1049 d->cell_next = d->cell_cur + i + 1;
1050 hb_log( "dvd: Skipping multi-angle cells %d-%d",
1056 d->cell_next = d->cell_cur + 1;
1060 /***********************************************************************
1062 ***********************************************************************
1064 **********************************************************************/
1065 static int dvdtime2msec(dvd_time_t * dt)
1067 double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
1068 double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
1070 ms = (((dt->hour & 0xf0) >> 3) * 5 + (dt->hour & 0x0f)) * 3600000;
1071 ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
1072 ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
1076 ms += ((dt->frame_u & 0x30) >> 3) * 5 +
1077 (dt->frame_u & 0x0f) * 1000.0 / fps;