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 /* Detect languages */
239 for( i = 0; i < vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
241 hb_audio_t * audio, * audio_tmp;
242 int audio_format, lang_code, audio_control,
244 iso639_lang_t * lang;
245 int lang_extension = 0;
247 hb_log( "scan: checking audio %d", i + 1 );
249 audio = calloc( sizeof( hb_audio_t ), 1 );
251 audio_format = vts->vtsi_mat->vts_audio_attr[i].audio_format;
252 lang_code = vts->vtsi_mat->vts_audio_attr[i].lang_code;
253 lang_extension = vts->vtsi_mat->vts_audio_attr[i].code_extension;
255 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i];
257 if( !( audio_control & 0x8000 ) )
259 hb_log( "scan: audio channel is not active" );
264 position = ( audio_control & 0x7F00 ) >> 8;
266 switch( audio_format )
269 audio->id = ( ( 0x80 + position ) << 8 ) | 0xbd;
270 audio->config.in.codec = HB_ACODEC_AC3;
275 audio->id = 0xc0 + position;
276 audio->config.in.codec = HB_ACODEC_MPGA;
280 audio->id = ( ( 0xa0 + position ) << 8 ) | 0xbd;
281 audio->config.in.codec = HB_ACODEC_LPCM;
285 audio->id = ( ( 0x88 + position ) << 8 ) | 0xbd;
286 audio->config.in.codec = HB_ACODEC_DCA;
291 audio->config.in.codec = 0;
292 hb_log( "scan: unknown audio codec (%x)",
301 /* Check for duplicate tracks */
303 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
305 audio_tmp = hb_list_item( title->list_audio, j );
306 if( audio->id == audio_tmp->id )
314 hb_log( "scan: duplicate audio track" );
319 audio->config.lang.type = lang_extension;
321 lang = lang_for_code( vts->vtsi_mat->vts_audio_attr[i].lang_code );
323 snprintf( audio->config.lang.description, sizeof( audio->config.lang.description ), "%s (%s)",
324 strlen(lang->native_name) ? lang->native_name : lang->eng_name,
325 audio->config.in.codec == HB_ACODEC_AC3 ? "AC3" : ( audio->config.in.codec ==
326 HB_ACODEC_DCA ? "DTS" : ( audio->config.in.codec ==
327 HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) ) );
328 snprintf( audio->config.lang.simple, sizeof( audio->config.lang.simple ), "%s",
329 strlen(lang->native_name) ? lang->native_name : lang->eng_name );
330 snprintf( audio->config.lang.iso639_2, sizeof( audio->config.lang.iso639_2 ), "%s",
333 switch( lang_extension )
339 strcat( audio->config.lang.description, " (Visually Impaired)" );
342 strcat( audio->config.lang.description, " (Director's Commentary 1)" );
345 strcat( audio->config.lang.description, " (Director's Commentary 2)" );
351 hb_log( "scan: id=%x, lang=%s, 3cc=%s ext=%i", audio->id,
352 audio->config.lang.description, audio->config.lang.iso639_2,
355 audio->config.in.track = i;
356 hb_list_add( title->list_audio, audio );
359 if( !hb_list_count( title->list_audio ) )
361 hb_log( "scan: ignoring title (no audio track)" );
365 memcpy( title->palette,
366 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette,
367 16 * sizeof( uint32_t ) );
369 /* Check for subtitles */
370 for( i = 0; i < vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
372 hb_subtitle_t * subtitle;
375 iso639_lang_t * lang;
376 int lang_extension = 0;
378 hb_log( "scan: checking subtitle %d", i + 1 );
381 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i];
383 if( !( spu_control & 0x80000000 ) )
385 hb_log( "scan: subtitle channel is not active" );
389 if( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
391 switch( vts->vtsi_mat->vts_video_attr.permitted_df )
394 position = spu_control & 0xFF;
397 position = ( spu_control >> 8 ) & 0xFF;
400 position = ( spu_control >> 16 ) & 0xFF;
405 position = ( spu_control >> 24 ) & 0x7F;
408 lang_extension = vts->vtsi_mat->vts_subp_attr[i].code_extension;
410 lang = lang_for_code( vts->vtsi_mat->vts_subp_attr[i].lang_code );
412 subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
413 subtitle->id = ( ( 0x20 + position ) << 8 ) | 0xbd;
414 snprintf( subtitle->lang, sizeof( subtitle->lang ), "%s",
415 strlen(lang->native_name) ? lang->native_name : lang->eng_name);
416 snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ), "%s",
419 subtitle->type = lang_extension;
421 switch( lang_extension )
428 strcat( subtitle->lang, " (Caption with bigger size character)");
431 strcat( subtitle->lang, " (Caption for Children)");
436 strcat( subtitle->lang, " (Closed Caption)");
439 strcat( subtitle->lang, " (Closed Caption with bigger size character)");
442 strcat( subtitle->lang, " (Closed Caption for Children)");
447 strcat( subtitle->lang, " (Forced Caption)");
456 strcat( subtitle->lang, " (Director's Commentary)");
459 strcat( subtitle->lang, " (Director's Commentary with bigger size character)");
462 strcat( subtitle->lang, " (Director's Commentary for Children)");
467 hb_log( "scan: id=%x, lang=%s, 3cc=%s", subtitle->id,
468 subtitle->lang, subtitle->iso639_2 );
470 hb_list_add( title->list_subtitle, subtitle );
474 hb_log( "scan: title %d has %d chapters", t,
475 vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts );
477 i < vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts; i++ )
479 int pgc_id_next, pgn_next;
482 chapter = calloc( sizeof( hb_chapter_t ), 1 );
483 /* remember the on-disc chapter number */
484 chapter->index = i + 1;
486 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgcn;
487 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgn;
488 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
491 chapter->cell_start = d->pgc->program_map[pgn-1] - 1;
492 chapter->block_start =
493 d->pgc->cell_playback[chapter->cell_start].first_sector;
496 if( i != vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts - 1 )
498 /* The cell before the starting cell of the next chapter,
500 pgc_id_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgcn;
501 pgn_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgn;
502 pgc_next = vts->vts_pgcit->pgci_srp[pgc_id_next-1].pgc;
503 chapter->cell_end = pgc_next->program_map[pgn_next-1] - 2;
504 if( chapter->cell_end < 0 )
513 /* ... the last cell of the title */
514 chapter->cell_end = title->cell_end;
517 d->pgc->cell_playback[chapter->cell_end].last_sector;
519 /* Block count, duration */
520 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
521 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
522 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
523 chapter->block_count = 0;
524 chapter->duration = 0;
526 d->cell_cur = chapter->cell_start;
527 while( d->cell_cur <= chapter->cell_end )
529 #define cp d->pgc->cell_playback[d->cell_cur]
530 chapter->block_count += cp.last_sector + 1 - cp.first_sector;
531 chapter->duration += 90LL * dvdtime2msec( &cp.playback_time );
534 d->cell_cur = d->cell_next;
536 hb_list_add( title->list_chapter, chapter );
540 /* The durations we get for chapters aren't precise. Scale them so
541 the total matches the title duration */
543 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
545 chapter = hb_list_item( title->list_chapter, i );
546 duration += chapter->duration;
548 duration_correction = (float) title->duration / (float) duration;
549 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
552 chapter = hb_list_item( title->list_chapter, i );
553 chapter->duration = duration_correction * chapter->duration;
554 seconds = ( chapter->duration + 45000 ) / 90000;
555 chapter->hours = seconds / 3600;
556 chapter->minutes = ( seconds % 3600 ) / 60;
557 chapter->seconds = seconds % 60;
559 hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %lld ms",
560 chapter->index, chapter->cell_start, chapter->cell_end,
561 chapter->block_start, chapter->block_end,
562 chapter->block_count, chapter->duration / 90 );
565 /* Get aspect. We don't get width/height/rate infos here as
566 they tend to be wrong */
567 switch( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
570 title->container_aspect = 4. / 3.;
573 title->container_aspect = 16. / 9.;
576 hb_log( "scan: unknown aspect" );
580 hb_log( "scan: aspect = %g", title->aspect );
582 /* This title is ok so far */
586 hb_list_close( &title->list_audio );
591 if( vts ) ifoClose( vts );
596 /***********************************************************************
598 ***********************************************************************
599 * Title and chapter start at 1
600 **********************************************************************/
601 int hb_dvd_start( hb_dvd_t * d, int title, int chapter )
606 /* Open the IFO and the VOBs for this title */
607 d->vts = d->vmg->tt_srpt->title[title-1].title_set_nr;
608 d->ttn = d->vmg->tt_srpt->title[title-1].vts_ttn;
609 if( !( d->ifo = ifoOpen( d->reader, d->vts ) ) )
611 hb_error( "dvd: ifoOpen failed for VTS %d", d->vts );
614 if( !( d->file = DVDOpenFile( d->reader, d->vts,
615 DVD_READ_TITLE_VOBS ) ) )
617 hb_error( "dvd: DVDOpenFile failed for VTS %d", d->vts );
621 /* Get title first and last blocks */
622 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgcn;
623 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgn;
624 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
625 d->cell_start = d->pgc->program_map[pgn - 1] - 1;
626 d->cell_end = d->pgc->nr_of_cells - 1;
627 d->title_start = d->pgc->cell_playback[d->cell_start].first_sector;
628 d->title_end = d->pgc->cell_playback[d->cell_end].last_sector;
631 d->title_block_count = 0;
632 for( i = d->cell_start; i <= d->cell_end; i++ )
634 d->title_block_count += d->pgc->cell_playback[i].last_sector + 1 -
635 d->pgc->cell_playback[i].first_sector;
638 /* Get pgc for the current chapter */
639 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgcn;
640 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgn;
641 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
643 /* Get the two first cells */
644 d->cell_cur = d->pgc->program_map[pgn-1] - 1;
647 d->block = d->pgc->cell_playback[d->cell_cur].first_sector;
648 d->next_vobu = d->block;
657 /***********************************************************************
659 ***********************************************************************
661 **********************************************************************/
662 void hb_dvd_stop( hb_dvd_t * d )
671 DVDCloseFile( d->file );
676 /***********************************************************************
678 ***********************************************************************
680 **********************************************************************/
681 int hb_dvd_seek( hb_dvd_t * d, float f )
686 count = f * d->title_block_count;
688 for( i = d->cell_start; i <= d->cell_end; i++ )
690 sizeCell = d->pgc->cell_playback[i].last_sector + 1 -
691 d->pgc->cell_playback[i].first_sector;
693 if( count < sizeCell )
699 /* Now let hb_dvd_read find the next VOBU */
700 d->next_vobu = d->pgc->cell_playback[i].first_sector + count;
708 if( i > d->cell_end )
714 * Assume that we are in sync, even if we are not given that it is obvious
715 * that we are out of sync if we have just done a seek.
723 /***********************************************************************
725 ***********************************************************************/
726 int is_nav_pack( unsigned char *buf )
729 * The NAV Pack is comprised of the PCI Packet and DSI Packet, both
730 * of these start at known offsets and start with a special identifier.
733 * PCI = { 00 00 01 bf # private stream header
738 * DSI = { 00 00 01 bf # private stream header
744 * The PCI starts at offset 0x26 into the sector, and the DSI starts at 0x400
746 * This information from: http://dvd.sourceforge.net/dvdinfo/
748 if( ( buf[0x26] == 0x00 && // PCI
752 buf[0x2c] == 0x00 ) &&
753 ( buf[0x400] == 0x00 && // DSI
754 buf[0x401] == 0x00 &&
755 buf[0x402] == 0x01 &&
756 buf[0x403] == 0xbf &&
757 buf[0x406] == 0x01 ) )
766 /***********************************************************************
768 ***********************************************************************
770 **********************************************************************/
771 int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
780 // if we've just finished the last cell of the title we don't
781 // want to read another block because our next_vobu pointer
782 // is probably invalid. Just return 'no data' & our caller
783 // should check and discover we're at eof.
784 if ( d->cell_cur > d->cell_end )
789 int block, pack_len, next_vobu, read_retry;
791 for( read_retry = 0; read_retry < 3; read_retry++ )
793 if( DVDReadBlocks( d->file, d->next_vobu, 1, b->data ) == 1 )
800 hb_log( "dvd: Read Error on blk %d, attempt %d",
801 d->next_vobu, read_retry );
805 if( read_retry == 3 )
807 hb_log( "dvd: vobu read error blk %d - skipping to cell %d",
808 d->next_vobu, d->cell_next );
809 d->cell_cur = d->cell_next;
810 if ( d->cell_cur > d->cell_end )
813 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
819 if ( !is_nav_pack( b->data ) ) {
821 if( d->in_sync == 1 ) {
822 hb_log("dvd: Lost sync, searching for NAV pack at blk %d",
829 navRead_DSI( &dsi_pack, &b->data[DSI_START_BYTE] );
831 if ( d->in_sync == 0 && d->cur_cell_id &&
832 (d->cur_vob_id != dsi_pack.dsi_gi.vobu_vob_idn ||
833 d->cur_cell_id != dsi_pack.dsi_gi.vobu_c_idn ) )
835 // We walked out of the cell we're supposed to be in.
836 // If we're not at the start of our next cell go there.
837 hb_log("dvd: left cell %d (%u,%u) for (%u,%u) at block %u",
838 d->cell_cur, d->cur_vob_id, d->cur_cell_id,
839 dsi_pack.dsi_gi.vobu_vob_idn, dsi_pack.dsi_gi.vobu_c_idn,
841 if ( d->next_vobu != d->pgc->cell_playback[d->cell_next].first_sector )
843 d->next_vobu = d->pgc->cell_playback[d->cell_next].first_sector;
849 block = dsi_pack.dsi_gi.nv_pck_lbn;
850 pack_len = dsi_pack.dsi_gi.vobu_ea;
852 // There are a total of 21 next vobu offsets (and 21 prev_vobu
853 // offsets) in the navpack SRI structure. The primary one is
854 // 'next_vobu' which is the offset (in dvd blocks) from the current
855 // block to the start of the next vobu. If the block at 'next_vobu'
856 // can't be read, 'next_video' is the offset to the vobu closest to it.
857 // The other 19 offsets are vobus at successively longer distances from
858 // the current block (this is so that a hardware player can do
859 // adaptive error recovery to skip over a bad spot on the disk). In all
860 // these offsets the high bit is set to indicate when it contains a
861 // valid offset. The next bit (2^30) is set to indicate that there's
862 // another valid offset in the SRI that's closer to the current block.
863 // A hardware player uses these bits to chose the closest valid offset
864 // and uses that as its next vobu. (Some mastering schemes appear to
865 // put a bogus offset in next_vobu with the 2^30 bit set & the
866 // correct offset in next_video. This works fine in hardware players
867 // but will mess up software that doesn't implement the full next
868 // vobu decision logic.) In addition to the flag bits there's a
869 // reserved value of the offset that indicates 'no next vobu' (which
870 // indicates the end of a cell). But having no next vobu pointer with a
871 // 'valid' bit set also indicates end of cell. Different mastering
872 // schemes seem to use all possible combinations of the flag bits
873 // and reserved values to indicate end of cell so we have to check
874 // them all or we'll get a disk read error from following an
876 uint32_t next_ptr = dsi_pack.vobu_sri.next_vobu;
877 if ( ( next_ptr & ( 1 << 31 ) ) == 0 ||
878 ( next_ptr & ( 1 << 30 ) ) != 0 ||
879 ( next_ptr & 0x3fffffff ) == 0x3fffffff )
881 next_ptr = dsi_pack.vobu_sri.next_video;
882 if ( ( next_ptr & ( 1 << 31 ) ) == 0 ||
883 ( next_ptr & 0x3fffffff ) == 0x3fffffff )
885 // don't have a valid forward pointer - assume end-of-cell
887 d->pack_len = pack_len;
891 next_vobu = block + ( next_ptr & 0x3fffffff );
895 block >= d->next_vobu &&
896 ( block <= d->title_start + d->title_block_count ||
897 block <= d->title_end ) )
900 d->pack_len = pack_len;
901 d->next_vobu = next_vobu;
905 /* Wasn't a valid VOBU, try next block */
908 hb_log( "dvd: couldn't find a VOBU after 1024 blocks" );
915 if( d->in_sync == 0 || d->in_sync == 2 )
917 if( d->in_sync == 0 )
919 hb_log( "dvd: In sync with DVD at block %d", d->block );
924 // Revert the cell overlap, and check for a chapter break
925 // If this cell is zero length (prev_vobu & next_vobu both
926 // set to END_OF_CELL) we need to check for beginning of
927 // cell before checking for end or we'll advance to the next
928 // cell too early and fail to generate a chapter mark when this
929 // cell starts a chapter.
930 if( ( dsi_pack.vobu_sri.prev_vobu & (1 << 31 ) ) == 0 ||
931 ( dsi_pack.vobu_sri.prev_vobu & 0x3fffffff ) == 0x3fffffff )
933 // A vobu that's not at the start of a cell can have an
934 // EOC prev pointer (this seems to be associated with some
935 // sort of drm). The rest of the content in the cell may be
936 // booby-trapped so treat this like an end-of-cell rather
937 // than a beginning of cell.
938 if ( d->pgc->cell_playback[d->cell_cur].first_sector < dsi_pack.dsi_gi.nv_pck_lbn &&
939 d->pgc->cell_playback[d->cell_cur].last_sector >= dsi_pack.dsi_gi.nv_pck_lbn )
941 hb_log( "dvd: null prev_vobu in cell %d at block %d", d->cell_cur,
943 // treat like end-of-cell then go directly to start of next cell.
944 d->cell_cur = d->cell_next;
946 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
953 if ( d->block != d->pgc->cell_playback[d->cell_cur].first_sector )
955 hb_log( "dvd: beginning of cell %d at block %d", d->cell_cur,
960 hb_error( "dvd: assuming missed end of cell %d", d->cell_cur );
961 d->cell_cur = d->cell_next;
962 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
969 d->cur_vob_id = dsi_pack.dsi_gi.vobu_vob_idn;
970 d->cur_cell_id = dsi_pack.dsi_gi.vobu_c_idn;
972 if( d->cell_overlap )
974 b->new_chap = hb_dvd_is_break( d );
980 if( ( dsi_pack.vobu_sri.next_vobu & (1 << 31 ) ) == 0 ||
981 ( dsi_pack.vobu_sri.next_vobu & 0x3fffffff ) == 0x3fffffff )
983 if ( d->block <= d->pgc->cell_playback[d->cell_cur].first_sector ||
984 d->block > d->pgc->cell_playback[d->cell_cur].last_sector )
986 hb_log( "dvd: end of cell %d at block %d", d->cell_cur,
989 d->cell_cur = d->cell_next;
991 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
999 if( DVDReadBlocks( d->file, d->block, 1, b->data ) != 1 )
1001 // this may be a real DVD error or may be DRM. Either way
1002 // we don't want to quit because of one bad block so set
1003 // things up so we'll advance to the next vobu and recurse.
1004 hb_error( "dvd: DVDReadBlocks failed (%d), skipping to vobu %u",
1005 d->block, d->next_vobu );
1007 goto top; /* XXX need to restructure this routine & avoid goto */
1017 /***********************************************************************
1019 ***********************************************************************
1020 * Returns in which chapter the next block to be read is.
1021 * Chapter numbers start at 1.
1022 **********************************************************************/
1023 int hb_dvd_chapter( hb_dvd_t * d )
1027 int nr_of_ptts = d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
1030 for( i = nr_of_ptts - 1;
1034 /* Get pgc for chapter (i+1) */
1035 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
1036 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
1037 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1039 if( d->cell_cur - d->cell_overlap >= pgc->program_map[pgn-1] - 1 &&
1040 d->cell_cur - d->cell_overlap <= pgc->nr_of_cells - 1 )
1042 /* We are in this chapter */
1051 /***********************************************************************
1053 ***********************************************************************
1054 * Returns chapter number if the current block is a new chapter start
1055 **********************************************************************/
1056 int hb_dvd_is_break( hb_dvd_t * d )
1060 int nr_of_ptts = d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
1064 for( i = nr_of_ptts - 1;
1068 /* Get pgc for chapter (i+1) */
1069 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
1070 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
1071 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1072 cell = pgc->program_map[pgn-1] - 1;
1074 if( cell <= d->cell_start )
1077 // This must not match against the start cell.
1078 if( pgc->cell_playback[cell].first_sector == d->block && cell != d->cell_start )
1087 /***********************************************************************
1089 ***********************************************************************
1090 * Closes and frees everything
1091 **********************************************************************/
1092 void hb_dvd_close( hb_dvd_t ** _d )
1102 DVDClose( d->reader );
1109 /***********************************************************************
1111 ***********************************************************************
1112 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
1113 * cell to be read when we will be done with cell_cur.
1114 **********************************************************************/
1115 static void FindNextCell( hb_dvd_t * d )
1119 if( d->pgc->cell_playback[d->cell_cur].block_type ==
1120 BLOCK_TYPE_ANGLE_BLOCK )
1123 while( d->pgc->cell_playback[d->cell_cur+i].block_mode !=
1124 BLOCK_MODE_LAST_CELL )
1128 d->cell_next = d->cell_cur + i + 1;
1129 hb_log( "dvd: Skipping multi-angle cells %d-%d",
1135 d->cell_next = d->cell_cur + 1;
1139 /***********************************************************************
1141 ***********************************************************************
1143 **********************************************************************/
1144 static int dvdtime2msec(dvd_time_t * dt)
1146 double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
1147 double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
1149 ms = (((dt->hour & 0xf0) >> 3) * 5 + (dt->hour & 0x0f)) * 3600000;
1150 ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
1151 ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
1155 ms += ((dt->frame_u & 0x30) >> 3) * 5 +
1156 (dt->frame_u & 0x0f) * 1000.0 / fps;