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.m0k.org/>.
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" );
182 title->cell_start = d->pgc->program_map[pgn-1] - 1;
183 title->block_start = d->pgc->cell_playback[title->cell_start].first_sector;
186 title->cell_end = d->pgc->nr_of_cells - 1;
187 title->block_end = d->pgc->cell_playback[title->cell_end].last_sector;
190 title->block_count = 0;
191 d->cell_cur = title->cell_start;
192 while( d->cell_cur <= title->cell_end )
194 #define cp d->pgc->cell_playback[d->cell_cur]
195 title->block_count += cp.last_sector + 1 - cp.first_sector;
198 d->cell_cur = d->cell_next;
201 hb_log( "scan: vts=%d, ttn=%d, cells=%d->%d, blocks=%d->%d, "
202 "%d blocks", title->vts, title->ttn, title->cell_start,
203 title->cell_end, title->block_start, title->block_end,
204 title->block_count );
207 title->duration = 90LL * dvdtime2msec( &d->pgc->playback_time );
208 title->hours = title->duration / 90000 / 3600;
209 title->minutes = ( ( title->duration / 90000 ) % 3600 ) / 60;
210 title->seconds = ( title->duration / 90000 ) % 60;
211 hb_log( "scan: duration is %02d:%02d:%02d (%lld ms)",
212 title->hours, title->minutes, title->seconds,
213 title->duration / 90 );
215 /* ignore titles under 10 seconds because they're often stills or
216 * clips with no audio & our preview code doesn't currently handle
217 * either of these. */
218 if( title->duration < 900000LL )
220 hb_log( "scan: ignoring title (too short)" );
224 /* Detect languages */
225 for( i = 0; i < vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
227 hb_audio_t * audio, * audio_tmp;
228 int audio_format, lang_code, audio_control,
230 iso639_lang_t * lang;
232 hb_log( "scan: checking audio %d", i + 1 );
234 audio = calloc( sizeof( hb_audio_t ), 1 );
236 audio_format = vts->vtsi_mat->vts_audio_attr[i].audio_format;
237 lang_code = vts->vtsi_mat->vts_audio_attr[i].lang_code;
239 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i];
241 if( !( audio_control & 0x8000 ) )
243 hb_log( "scan: audio channel is not active" );
248 position = ( audio_control & 0x7F00 ) >> 8;
250 switch( audio_format )
253 audio->id = ( ( 0x80 + position ) << 8 ) | 0xbd;
254 audio->codec = HB_ACODEC_AC3;
259 audio->id = 0xc0 + position;
260 audio->codec = HB_ACODEC_MPGA;
264 audio->id = ( ( 0xa0 + position ) << 8 ) | 0xbd;
265 audio->codec = HB_ACODEC_LPCM;
269 audio->id = ( ( 0x88 + position ) << 8 ) | 0xbd;
270 audio->codec = HB_ACODEC_DCA;
276 hb_log( "scan: unknown audio codec (%x)",
285 /* Check for duplicate tracks */
287 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
289 audio_tmp = hb_list_item( title->list_audio, j );
290 if( audio->id == audio_tmp->id )
298 hb_log( "scan: duplicate audio track" );
303 lang = lang_for_code( vts->vtsi_mat->vts_audio_attr[i].lang_code );
305 snprintf( audio->lang, sizeof( audio->lang ), "%s (%s)",
306 strlen(lang->native_name) ? lang->native_name : lang->eng_name,
307 audio->codec == HB_ACODEC_AC3 ? "AC3" : ( audio->codec ==
308 HB_ACODEC_DCA ? "DTS" : ( audio->codec ==
309 HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) ) );
310 snprintf( audio->lang_simple, sizeof( audio->lang_simple ), "%s",
311 strlen(lang->native_name) ? lang->native_name : lang->eng_name );
312 snprintf( audio->iso639_2, sizeof( audio->iso639_2 ), "%s",
315 hb_log( "scan: id=%x, lang=%s, 3cc=%s", audio->id,
316 audio->lang, audio->iso639_2 );
318 hb_list_add( title->list_audio, audio );
321 if( !hb_list_count( title->list_audio ) )
323 hb_log( "scan: ignoring title (no audio track)" );
327 memcpy( title->palette,
328 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette,
329 16 * sizeof( uint32_t ) );
331 /* Check for subtitles */
332 for( i = 0; i < vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
334 hb_subtitle_t * subtitle;
337 iso639_lang_t * lang;
339 hb_log( "scan: checking subtitle %d", i + 1 );
342 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i];
344 if( !( spu_control & 0x80000000 ) )
346 hb_log( "scan: subtitle channel is not active" );
350 if( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
352 switch( vts->vtsi_mat->vts_video_attr.permitted_df )
355 position = spu_control & 0xFF;
358 position = ( spu_control >> 8 ) & 0xFF;
361 position = ( spu_control >> 16 ) & 0xFF;
366 position = ( spu_control >> 24 ) & 0x7F;
369 lang = lang_for_code( vts->vtsi_mat->vts_subp_attr[i].lang_code );
371 subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
372 subtitle->id = ( ( 0x20 + position ) << 8 ) | 0xbd;
373 snprintf( subtitle->lang, sizeof( subtitle->lang ), "%s",
374 strlen(lang->native_name) ? lang->native_name : lang->eng_name);
375 snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ), "%s",
378 hb_log( "scan: id=%x, lang=%s, 3cc=%s", subtitle->id,
379 subtitle->lang, subtitle->iso639_2 );
381 hb_list_add( title->list_subtitle, subtitle );
385 hb_log( "scan: title %d has %d chapters", t,
386 vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts );
388 i < vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts; i++ )
390 int pgc_id_next, pgn_next;
393 chapter = calloc( sizeof( hb_chapter_t ), 1 );
394 /* remember the on-disc chapter number */
395 chapter->index = i + 1;
397 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgcn;
398 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgn;
399 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
402 chapter->cell_start = d->pgc->program_map[pgn-1] - 1;
403 chapter->block_start =
404 d->pgc->cell_playback[chapter->cell_start].first_sector;
407 if( i != vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts - 1 )
409 /* The cell before the starting cell of the next chapter,
411 pgc_id_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgcn;
412 pgn_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgn;
413 pgc_next = vts->vts_pgcit->pgci_srp[pgc_id_next-1].pgc;
414 chapter->cell_end = pgc_next->program_map[pgn_next-1] - 2;
415 if( chapter->cell_end < 0 )
424 /* ... the last cell of the title */
425 chapter->cell_end = title->cell_end;
428 d->pgc->cell_playback[chapter->cell_end].last_sector;
430 /* Block count, duration */
431 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
432 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
433 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
434 chapter->block_count = 0;
435 chapter->duration = 0;
437 d->cell_cur = chapter->cell_start;
438 while( d->cell_cur <= chapter->cell_end )
440 #define cp d->pgc->cell_playback[d->cell_cur]
441 chapter->block_count += cp.last_sector + 1 - cp.first_sector;
442 chapter->duration += 90LL * dvdtime2msec( &cp.playback_time );
445 d->cell_cur = d->cell_next;
447 hb_list_add( title->list_chapter, chapter );
451 /* The durations we get for chapters aren't precise. Scale them so
452 the total matches the title duration */
454 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
456 chapter = hb_list_item( title->list_chapter, i );
457 duration += chapter->duration;
459 duration_correction = (float) title->duration / (float) duration;
460 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
463 chapter = hb_list_item( title->list_chapter, i );
464 chapter->duration = duration_correction * chapter->duration;
465 seconds = ( chapter->duration + 45000 ) / 90000;
466 chapter->hours = seconds / 3600;
467 chapter->minutes = ( seconds % 3600 ) / 60;
468 chapter->seconds = seconds % 60;
470 hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %lld ms",
471 chapter->index, chapter->cell_start, chapter->cell_end,
472 chapter->block_start, chapter->block_end,
473 chapter->block_count, chapter->duration / 90 );
476 /* Get aspect. We don't get width/height/rate infos here as
477 they tend to be wrong */
478 switch( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
481 title->aspect = HB_ASPECT_BASE * 4 / 3;
484 title->aspect = HB_ASPECT_BASE * 16 / 9;
487 hb_log( "scan: unknown aspect" );
491 hb_log( "scan: aspect = %d", title->aspect );
493 /* This title is ok so far */
497 hb_list_close( &title->list_audio );
502 if( vts ) ifoClose( vts );
507 /***********************************************************************
509 ***********************************************************************
510 * Title and chapter start at 1
511 **********************************************************************/
512 int hb_dvd_start( hb_dvd_t * d, int title, int chapter )
517 /* Open the IFO and the VOBs for this title */
518 d->vts = d->vmg->tt_srpt->title[title-1].title_set_nr;
519 d->ttn = d->vmg->tt_srpt->title[title-1].vts_ttn;
520 if( !( d->ifo = ifoOpen( d->reader, d->vts ) ) )
522 hb_error( "dvd: ifoOpen failed for VTS %d", d->vts );
525 if( !( d->file = DVDOpenFile( d->reader, d->vts,
526 DVD_READ_TITLE_VOBS ) ) )
528 hb_error( "dvd: DVDOpenFile failed for VTS %d", d->vts );
532 /* Get title first and last blocks */
533 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgcn;
534 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgn;
535 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
536 d->cell_start = d->pgc->program_map[pgn - 1] - 1;
537 d->cell_end = d->pgc->nr_of_cells - 1;
538 d->title_start = d->pgc->cell_playback[d->cell_start].first_sector;
539 d->title_end = d->pgc->cell_playback[d->cell_end].last_sector;
542 d->title_block_count = 0;
543 for( i = d->cell_start; i <= d->cell_end; i++ )
545 d->title_block_count += d->pgc->cell_playback[i].last_sector + 1 -
546 d->pgc->cell_playback[i].first_sector;
549 /* Get pgc for the current chapter */
550 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgcn;
551 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgn;
552 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
554 /* Get the two first cells */
555 d->cell_cur = d->pgc->program_map[pgn-1] - 1;
558 d->block = d->pgc->cell_playback[d->cell_cur].first_sector;
559 d->next_vobu = d->block;
568 /***********************************************************************
570 ***********************************************************************
572 **********************************************************************/
573 void hb_dvd_stop( hb_dvd_t * d )
582 DVDCloseFile( d->file );
587 /***********************************************************************
589 ***********************************************************************
591 **********************************************************************/
592 int hb_dvd_seek( hb_dvd_t * d, float f )
597 count = f * d->title_block_count;
599 for( i = d->cell_start; i <= d->cell_end; i++ )
601 sizeCell = d->pgc->cell_playback[i].last_sector + 1 -
602 d->pgc->cell_playback[i].first_sector;
604 if( count < sizeCell )
610 /* Now let hb_dvd_read find the next VOBU */
611 d->next_vobu = d->pgc->cell_playback[i].first_sector + count;
619 if( i > d->cell_end )
625 * Assume that we are in sync, even if we are not given that it is obvious
626 * that we are out of sync if we have just done a seek.
634 /***********************************************************************
636 ***********************************************************************/
637 int is_nav_pack( unsigned char *buf )
640 * The NAV Pack is comprised of the PCI Packet and DSI Packet, both
641 * of these start at known offsets and start with a special identifier.
644 * PCI = { 00 00 01 bf # private stream header
649 * DSI = { 00 00 01 bf # private stream header
655 * The PCI starts at offset 0x26 into the sector, and the DSI starts at 0x400
657 * This information from: http://dvd.sourceforge.net/dvdinfo/
659 if( ( buf[0x26] == 0x00 && // PCI
663 buf[0x2c] == 0x00 ) &&
664 ( buf[0x400] == 0x00 && // DSI
665 buf[0x401] == 0x00 &&
666 buf[0x402] == 0x01 &&
667 buf[0x403] == 0xbf &&
668 buf[0x406] == 0x01 ) )
677 /***********************************************************************
679 ***********************************************************************
681 **********************************************************************/
682 int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
691 // if we've just finished the last cell of the title we don't
692 // want to read another block because our next_vobu pointer
693 // is probably invalid. Just return 'no data' & our caller
694 // should check and discover we're at eof.
695 if ( d->cell_cur > d->cell_end )
700 int block, pack_len, next_vobu, read_retry;
702 for( read_retry = 0; read_retry < 3; read_retry++ )
704 if( DVDReadBlocks( d->file, d->next_vobu, 1, b->data ) == 1 )
711 hb_log( "dvd: Read Error on blk %d, attempt %d",
712 d->next_vobu, read_retry );
716 if( read_retry == 3 )
718 hb_log( "dvd: vobu read error blk %d - skipping to cell %d",
719 d->next_vobu, d->cell_next );
720 d->cell_cur = d->cell_next;
721 if ( d->cell_cur > d->cell_end )
724 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
730 if ( !is_nav_pack( b->data ) ) {
732 if( d->in_sync == 1 ) {
733 hb_log("dvd: Lost sync, searching for NAV pack at blk %d",
740 navRead_DSI( &dsi_pack, &b->data[DSI_START_BYTE] );
742 if ( d->in_sync == 0 && d->cur_cell_id &&
743 (d->cur_vob_id != dsi_pack.dsi_gi.vobu_vob_idn ||
744 d->cur_cell_id != dsi_pack.dsi_gi.vobu_c_idn ) )
746 // We walked out of the cell we're supposed to be in.
747 // If we're not at the start of our next cell go there.
748 hb_log("dvd: left cell %d (%u,%u) for (%u,%u) at block %u",
749 d->cell_cur, d->cur_vob_id, d->cur_cell_id,
750 dsi_pack.dsi_gi.vobu_vob_idn, dsi_pack.dsi_gi.vobu_c_idn,
752 if ( d->next_vobu != d->pgc->cell_playback[d->cell_next].first_sector )
754 d->next_vobu = d->pgc->cell_playback[d->cell_next].first_sector;
760 block = dsi_pack.dsi_gi.nv_pck_lbn;
761 pack_len = dsi_pack.dsi_gi.vobu_ea;
763 // There are a total of 21 next vobu offsets (and 21 prev_vobu
764 // offsets) in the navpack SRI structure. The primary one is
765 // 'next_vobu' which is the offset (in dvd blocks) from the current
766 // block to the start of the next vobu. If the block at 'next_vobu'
767 // can't be read, 'next_video' is the offset to the vobu closest to it.
768 // The other 19 offsets are vobus at successively longer distances from
769 // the current block (this is so that a hardware player can do
770 // adaptive error recovery to skip over a bad spot on the disk). In all
771 // these offsets the high bit is set to indicate when it contains a
772 // valid offset. The next bit (2^30) is set to indicate that there's
773 // another valid offset in the SRI that's closer to the current block.
774 // A hardware player uses these bits to chose the closest valid offset
775 // and uses that as its next vobu. (Some mastering schemes appear to
776 // put a bogus offset in next_vobu with the 2^30 bit set & the
777 // correct offset in next_video. This works fine in hardware players
778 // but will mess up software that doesn't implement the full next
779 // vobu decision logic.) In addition to the flag bits there's a
780 // reserved value of the offset that indicates 'no next vobu' (which
781 // indicates the end of a cell). But having no next vobu pointer with a
782 // 'valid' bit set also indicates end of cell. Different mastering
783 // schemes seem to use all possible combinations of the flag bits
784 // and reserved values to indicate end of cell so we have to check
785 // them all or we'll get a disk read error from following an
787 uint32_t next_ptr = dsi_pack.vobu_sri.next_vobu;
788 if ( ( next_ptr & ( 1 << 31 ) ) == 0 ||
789 ( next_ptr & ( 1 << 30 ) ) != 0 ||
790 ( next_ptr & 0x3fffffff ) == 0x3fffffff )
792 next_ptr = dsi_pack.vobu_sri.next_video;
793 if ( ( next_ptr & ( 1 << 31 ) ) == 0 ||
794 ( next_ptr & 0x3fffffff ) == 0x3fffffff )
796 // don't have a valid forward pointer - assume end-of-cell
798 d->pack_len = pack_len;
802 next_vobu = block + ( next_ptr & 0x3fffffff );
806 block >= d->next_vobu &&
807 ( block <= d->title_start + d->title_block_count ||
808 block <= d->title_end ) )
811 d->pack_len = pack_len;
812 d->next_vobu = next_vobu;
816 /* Wasn't a valid VOBU, try next block */
819 hb_log( "dvd: couldn't find a VOBU after 1024 blocks" );
826 if( d->in_sync == 0 || d->in_sync == 2 )
828 if( d->in_sync == 0 )
830 hb_log( "dvd: In sync with DVD at block %d", d->block );
835 // Revert the cell overlap, and check for a chapter break
836 // If this cell is zero length (prev_vobu & next_vobu both
837 // set to END_OF_CELL) we need to check for beginning of
838 // cell before checking for end or we'll advance to the next
839 // cell too early and fail to generate a chapter mark when this
840 // cell starts a chapter.
841 if( ( dsi_pack.vobu_sri.prev_vobu & (1 << 31 ) ) == 0 ||
842 ( dsi_pack.vobu_sri.prev_vobu & 0x3fffffff ) == 0x3fffffff )
844 // A vobu that's not at the start of a cell can have an
845 // EOC prev pointer (this seems to be associated with some
846 // sort of drm). The rest of the content in the cell may be
847 // booby-trapped so treat this like an end-of-cell rather
848 // than a beginning of cell.
849 if ( d->pgc->cell_playback[d->cell_cur].first_sector < dsi_pack.dsi_gi.nv_pck_lbn &&
850 d->pgc->cell_playback[d->cell_cur].last_sector >= dsi_pack.dsi_gi.nv_pck_lbn )
852 hb_log( "dvd: null prev_vobu in cell %d at block %d", d->cell_cur,
854 // treat like end-of-cell then go directly to start of next cell.
855 d->cell_cur = d->cell_next;
857 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
864 hb_log( "dvd: Beginning of Cell (%d) at block %d", d->cell_cur,
868 hb_error( "dvd: Assuming missed End of Cell (%d)", d->cell_cur );
869 d->cell_cur = d->cell_next;
870 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
877 d->cur_vob_id = dsi_pack.dsi_gi.vobu_vob_idn;
878 d->cur_cell_id = dsi_pack.dsi_gi.vobu_c_idn;
880 if( d->cell_overlap )
882 b->new_chap = hb_dvd_is_break( d );
888 if( ( dsi_pack.vobu_sri.next_vobu & (1 << 31 ) ) == 0 ||
889 ( dsi_pack.vobu_sri.next_vobu & 0x3fffffff ) == 0x3fffffff )
891 hb_log( "dvd: End of Cell (%d) at block %d", d->cell_cur,
893 d->cell_cur = d->cell_next;
895 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
903 if( DVDReadBlocks( d->file, d->block, 1, b->data ) != 1 )
905 // this may be a real DVD error or may be DRM. Either way
906 // we don't want to quit because of one bad block so set
907 // things up so we'll advance to the next vobu and recurse.
908 hb_error( "dvd: DVDReadBlocks failed (%d), skipping to vobu %u",
909 d->block, d->next_vobu );
911 goto top; /* XXX need to restructure this routine & avoid goto */
921 /***********************************************************************
923 ***********************************************************************
924 * Returns in which chapter the next block to be read is.
925 * Chapter numbers start at 1.
926 **********************************************************************/
927 int hb_dvd_chapter( hb_dvd_t * d )
931 int nr_of_ptts = d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
934 for( i = nr_of_ptts - 1;
938 /* Get pgc for chapter (i+1) */
939 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
940 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
941 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
943 if( d->cell_cur - d->cell_overlap >= pgc->program_map[pgn-1] - 1 &&
944 d->cell_cur - d->cell_overlap <= pgc->nr_of_cells - 1 )
946 /* We are in this chapter */
955 /***********************************************************************
957 ***********************************************************************
958 * Returns 1 if the current block is a new chapter start
959 **********************************************************************/
960 int hb_dvd_is_break( hb_dvd_t * d )
964 int nr_of_ptts = d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
968 for( i = nr_of_ptts - 1;
972 /* Get pgc for chapter (i+1) */
973 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
974 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
975 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
976 cell = pgc->program_map[pgn-1] - 1;
978 if( cell <= d->cell_start )
981 // This must not match against the start cell.
982 if( pgc->cell_playback[cell].first_sector == d->block && cell != d->cell_start )
984 hb_log("dvd: Chapter Break Cell Found");
992 /***********************************************************************
994 ***********************************************************************
995 * Closes and frees everything
996 **********************************************************************/
997 void hb_dvd_close( hb_dvd_t ** _d )
1007 DVDClose( d->reader );
1014 /***********************************************************************
1016 ***********************************************************************
1017 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1018 * cell to be read when we will be done with cell_cur.
1019 **********************************************************************/
1020 static void FindNextCell( hb_dvd_t * d )
1024 if( d->pgc->cell_playback[d->cell_cur].block_type ==
1025 BLOCK_TYPE_ANGLE_BLOCK )
1028 while( d->pgc->cell_playback[d->cell_cur+i].block_mode !=
1029 BLOCK_MODE_LAST_CELL )
1033 d->cell_next = d->cell_cur + i + 1;
1034 hb_log( "dvd: Skipping multi-angle cells %d-%d",
1040 d->cell_next = d->cell_cur + 1;
1044 /***********************************************************************
1046 ***********************************************************************
1048 **********************************************************************/
1049 static int dvdtime2msec(dvd_time_t * dt)
1051 double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
1052 double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
1054 ms = (((dt->hour & 0xf0) >> 3) * 5 + (dt->hour & 0x0f)) * 3600000;
1055 ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
1056 ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
1060 ms += ((dt->frame_u & 0x30) >> 3) * 5 +
1061 (dt->frame_u & 0x0f) * 1000.0 / fps;