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;
39 /***********************************************************************
41 **********************************************************************/
42 static void FindNextCell( hb_dvd_t * );
43 static int dvdtime2msec( dvd_time_t * );
45 char * hb_dvd_name( char * path )
47 static char name[1024];
48 unsigned char unused[1024];
49 dvd_reader_t * reader;
51 reader = DVDOpen( path );
57 if( DVDUDFVolumeInfo( reader, name, sizeof( name ),
58 unused, sizeof( unused ) ) )
68 /***********************************************************************
70 ***********************************************************************
72 **********************************************************************/
73 hb_dvd_t * hb_dvd_init( char * path )
77 d = calloc( sizeof( hb_dvd_t ), 1 );
80 if( !( d->reader = DVDOpen( path ) ) )
82 hb_error( "dvd: DVDOpen failed (%s)", path );
87 if( !( d->vmg = ifoOpen( d->reader, 0 ) ) )
89 hb_error( "dvd: ifoOpen failed" );
93 d->path = strdup( path );
98 if( d->vmg ) ifoClose( d->vmg );
99 if( d->reader ) DVDClose( d->reader );
104 /***********************************************************************
106 **********************************************************************/
107 int hb_dvd_title_count( hb_dvd_t * d )
109 return d->vmg->tt_srpt->nr_of_srpts;
112 /***********************************************************************
114 **********************************************************************/
115 hb_title_t * hb_dvd_title_scan( hb_dvd_t * d, int t )
119 ifo_handle_t * vts = NULL;
121 hb_chapter_t * chapter, * chapter_old;
124 float duration_correction;
125 unsigned char unused[1024];
127 hb_log( "scan: scanning title %d", t );
129 title = hb_title_init( d->path, t );
130 if( DVDUDFVolumeInfo( d->reader, title->name, sizeof( title->name ),
131 unused, sizeof( unused ) ) )
133 char * p_cur, * p_last = d->path;
134 for( p_cur = d->path; *p_cur; p_cur++ )
136 if( p_cur[0] == '/' && p_cur[1] )
141 snprintf( title->name, sizeof( title->name ), "%s", p_last );
144 /* VTS which our title is in */
145 title->vts = d->vmg->tt_srpt->title[t-1].title_set_nr;
147 hb_log( "scan: opening IFO for VTS %d", title->vts );
148 if( !( vts = ifoOpen( d->reader, title->vts ) ) )
150 hb_error( "scan: ifoOpen failed" );
154 /* Position of the title in the VTS */
155 title->ttn = d->vmg->tt_srpt->title[t-1].vts_ttn;
158 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
159 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
160 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
162 hb_log("pgc_id: %d, pgn: %d: pgc: 0x%x", pgc_id, pgn, d->pgc);
166 hb_error( "scan: pgc not valid, skipping" );
171 title->cell_start = d->pgc->program_map[pgn-1] - 1;
172 title->block_start = d->pgc->cell_playback[title->cell_start].first_sector;
175 title->cell_end = d->pgc->nr_of_cells - 1;
176 title->block_end = d->pgc->cell_playback[title->cell_end].last_sector;
179 title->block_count = 0;
180 d->cell_cur = title->cell_start;
181 while( d->cell_cur <= title->cell_end )
183 #define cp d->pgc->cell_playback[d->cell_cur]
184 title->block_count += cp.last_sector + 1 - cp.first_sector;
187 d->cell_cur = d->cell_next;
190 hb_log( "scan: vts=%d, ttn=%d, cells=%d->%d, blocks=%d->%d, "
191 "%d blocks", title->vts, title->ttn, title->cell_start,
192 title->cell_end, title->block_start, title->block_end,
193 title->block_count );
195 if( title->block_count < 2048 )
197 hb_log( "scan: title too short (%d blocks), ignoring",
198 title->block_count );
204 title->duration = 90LL * dvdtime2msec( &d->pgc->playback_time );
205 title->hours = title->duration / 90000 / 3600;
206 title->minutes = ( ( title->duration / 90000 ) % 3600 ) / 60;
207 title->seconds = ( title->duration / 90000 ) % 60;
208 hb_log( "scan: duration is %02d:%02d:%02d (%lld ms)",
209 title->hours, title->minutes, title->seconds,
210 title->duration / 90 );
212 /* Discard titles under 10 seconds */
213 if( !( title->hours | title->minutes ) && title->seconds < 10 )
215 hb_log( "scan: ignoring title (too short)" );
219 /* Detect languages */
220 for( i = 0; i < vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
222 hb_audio_t * audio, * audio_tmp;
223 int audio_format, lang_code, audio_control,
225 iso639_lang_t * lang;
227 hb_log( "scan: checking audio %d", i + 1 );
229 audio = calloc( sizeof( hb_audio_t ), 1 );
231 audio_format = vts->vtsi_mat->vts_audio_attr[i].audio_format;
232 lang_code = vts->vtsi_mat->vts_audio_attr[i].lang_code;
234 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i];
236 if( !( audio_control & 0x8000 ) )
238 hb_log( "scan: audio channel is not active" );
243 position = ( audio_control & 0x7F00 ) >> 8;
245 switch( audio_format )
248 audio->id = ( ( 0x80 + position ) << 8 ) | 0xbd;
249 audio->codec = HB_ACODEC_AC3;
254 audio->id = 0xc0 + position;
255 audio->codec = HB_ACODEC_MPGA;
259 audio->id = ( ( 0xa0 + position ) << 8 ) | 0xbd;
260 audio->codec = HB_ACODEC_LPCM;
264 audio->id = ( ( 0x88 + position ) << 8 ) | 0xbd;
265 audio->codec = HB_ACODEC_DCA;
271 hb_log( "scan: unknown audio codec (%x)",
280 /* Check for duplicate tracks */
282 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
284 audio_tmp = hb_list_item( title->list_audio, j );
285 if( audio->id == audio_tmp->id )
293 hb_log( "scan: duplicate audio track" );
298 lang = lang_for_code( vts->vtsi_mat->vts_audio_attr[i].lang_code );
300 snprintf( audio->lang, sizeof( audio->lang ), "%s (%s)",
301 strlen(lang->native_name) ? lang->native_name : lang->eng_name,
302 audio->codec == HB_ACODEC_AC3 ? "AC3" : ( audio->codec ==
303 HB_ACODEC_DCA ? "DTS" : ( audio->codec ==
304 HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) ) );
305 snprintf( audio->lang_simple, sizeof( audio->lang_simple ), "%s",
306 strlen(lang->native_name) ? lang->native_name : lang->eng_name );
307 snprintf( audio->iso639_2, sizeof( audio->iso639_2 ), "%s",
310 hb_log( "scan: id=%x, lang=%s, 3cc=%s", audio->id,
311 audio->lang, audio->iso639_2 );
313 hb_list_add( title->list_audio, audio );
316 if( !hb_list_count( title->list_audio ) )
318 hb_log( "scan: ignoring title (no audio track)" );
322 memcpy( title->palette,
323 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette,
324 16 * sizeof( uint32_t ) );
326 /* Check for subtitles */
327 for( i = 0; i < vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
329 hb_subtitle_t * subtitle;
332 iso639_lang_t * lang;
334 hb_log( "scan: checking subtitle %d", i + 1 );
337 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i];
339 if( !( spu_control & 0x80000000 ) )
341 hb_log( "scan: subtitle channel is not active" );
345 if( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
347 switch( vts->vtsi_mat->vts_video_attr.permitted_df )
350 position = spu_control & 0xFF;
353 position = ( spu_control >> 8 ) & 0xFF;
356 position = ( spu_control >> 16 ) & 0xFF;
361 position = ( spu_control >> 24 ) & 0x7F;
364 lang = lang_for_code( vts->vtsi_mat->vts_subp_attr[i].lang_code );
366 subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
367 subtitle->id = ( ( 0x20 + position ) << 8 ) | 0xbd;
368 snprintf( subtitle->lang, sizeof( subtitle->lang ), "%s",
369 strlen(lang->native_name) ? lang->native_name : lang->eng_name);
370 snprintf( subtitle->iso639_2, sizeof( subtitle->iso639_2 ), "%s",
373 hb_log( "scan: id=%x, lang=%s, 3cc=%s", subtitle->id,
374 subtitle->lang, subtitle->iso639_2 );
376 hb_list_add( title->list_subtitle, subtitle );
380 hb_log( "scan: title %d has %d chapters", t,
381 vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts );
383 i < vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts; i++ )
385 int pgc_id_next, pgn_next;
388 chapter = calloc( sizeof( hb_chapter_t ), 1 );
391 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgcn;
392 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgn;
393 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
396 chapter->cell_start = d->pgc->program_map[pgn-1] - 1;
397 chapter->block_start =
398 d->pgc->cell_playback[chapter->cell_start].first_sector;
401 if( i != vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts - 1 )
403 /* The cell before the starting cell of the next chapter,
405 pgc_id_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgcn;
406 pgn_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgn;
407 pgc_next = vts->vts_pgcit->pgci_srp[pgc_id_next-1].pgc;
408 chapter->cell_end = pgc_next->program_map[pgn_next-1] - 2;
409 if( chapter->cell_end < 0 )
418 /* ... the last cell of the title */
419 chapter->cell_end = title->cell_end;
422 d->pgc->cell_playback[chapter->cell_end].last_sector;
424 /* Block count, duration */
425 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
426 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
427 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
428 chapter->block_count = 0;
429 chapter->duration = 0;
431 d->cell_cur = chapter->cell_start;
432 while( d->cell_cur <= chapter->cell_end )
434 #define cp d->pgc->cell_playback[d->cell_cur]
435 chapter->block_count += cp.last_sector + 1 - cp.first_sector;
436 chapter->duration += 90LL * dvdtime2msec( &cp.playback_time );
439 d->cell_cur = d->cell_next;
442 if( chapter->block_count < 2048 && chapter->index > 1 )
444 hb_log( "scan: chapter %d too short (%d blocks, "
445 "cells=%d->%d), merging", chapter->index,
446 chapter->block_count, chapter->cell_start,
448 chapter_old = hb_list_item( title->list_chapter,
449 chapter->index - 2 );
450 chapter_old->cell_end = chapter->cell_end;
451 chapter_old->block_end = chapter->block_end;
452 chapter_old->block_count += chapter->block_count;
454 chapter = chapter_old;
458 hb_list_add( title->list_chapter, chapter );
463 /* The durations we get for chapters aren't precise. Scale them so
464 the total matches the title duration */
466 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
468 chapter = hb_list_item( title->list_chapter, i );
469 duration += chapter->duration;
471 duration_correction = (float) title->duration / (float) duration;
472 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
475 chapter = hb_list_item( title->list_chapter, i );
476 chapter->duration = duration_correction * chapter->duration;
477 seconds = ( chapter->duration + 45000 ) / 90000;
478 chapter->hours = seconds / 3600;
479 chapter->minutes = ( seconds % 3600 ) / 60;
480 chapter->seconds = seconds % 60;
482 hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %lld ms",
483 chapter->index, chapter->cell_start, chapter->cell_end,
484 chapter->block_start, chapter->block_end,
485 chapter->block_count, chapter->duration / 90 );
488 /* Get aspect. We don't get width/height/rate infos here as
489 they tend to be wrong */
490 switch( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
493 title->aspect = HB_ASPECT_BASE * 4 / 3;
496 title->aspect = HB_ASPECT_BASE * 16 / 9;
499 hb_log( "scan: unknown aspect" );
503 hb_log( "scan: aspect = %d", title->aspect );
505 /* This title is ok so far */
509 hb_list_close( &title->list_audio );
514 if( vts ) ifoClose( vts );
519 /***********************************************************************
521 ***********************************************************************
522 * Title and chapter start at 1
523 **********************************************************************/
524 int hb_dvd_start( hb_dvd_t * d, int title, int chapter )
529 /* Open the IFO and the VOBs for this title */
530 d->vts = d->vmg->tt_srpt->title[title-1].title_set_nr;
531 d->ttn = d->vmg->tt_srpt->title[title-1].vts_ttn;
532 if( !( d->ifo = ifoOpen( d->reader, d->vts ) ) )
534 hb_error( "dvd: ifoOpen failed for VTS %d", d->vts );
537 if( !( d->file = DVDOpenFile( d->reader, d->vts,
538 DVD_READ_TITLE_VOBS ) ) )
540 hb_error( "dvd: DVDOpenFile failed for VTS %d", d->vts );
544 /* Get title first and last blocks */
545 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgcn;
546 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgn;
547 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
548 d->cell_start = d->pgc->program_map[pgn - 1] - 1;
549 d->cell_end = d->pgc->nr_of_cells - 1;
550 d->title_start = d->pgc->cell_playback[d->cell_start].first_sector;
551 d->title_end = d->pgc->cell_playback[d->cell_end].last_sector;
554 d->title_block_count = 0;
555 for( i = d->cell_start; i <= d->cell_end; i++ )
557 d->title_block_count += d->pgc->cell_playback[i].last_sector + 1 -
558 d->pgc->cell_playback[i].first_sector;
561 /* Get pgc for the current chapter */
562 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgcn;
563 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgn;
564 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
566 /* Get the two first cells */
567 d->cell_cur = d->pgc->program_map[pgn-1] - 1;
570 d->block = d->pgc->cell_playback[d->cell_cur].first_sector;
571 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 )
619 /* Now let hb_dvd_read find the next VOBU */
620 d->next_vobu = d->pgc->cell_playback[i].first_sector + count;
628 if( i > d->cell_end )
637 /***********************************************************************
639 ***********************************************************************
640 * Pretty much directly lifted from libdvdread's play_title function.
641 **********************************************************************/
642 int is_nav_pack( unsigned char *buf )
644 return ( buf[41] == 0xbf && buf[1027] == 0xbf );
648 /***********************************************************************
650 ***********************************************************************
652 **********************************************************************/
653 int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
665 int block, pack_len, next_vobu, read_retry;
667 for( read_retry = 0; read_retry < 3; read_retry++ )
669 if( DVDReadBlocks( d->file, d->next_vobu, 1, b->data ) == 1 )
676 hb_log( "dvd: Read Error on blk %d, attempt %d",
677 d->next_vobu, read_retry );
681 if( read_retry == 3 )
683 hb_log( "dvd: Unrecoverable Read Error from DVD, potential HD or DVD Failure (blk: %d)", d->next_vobu );
687 if ( !is_nav_pack( b->data ) ) {
692 navRead_DSI( &dsi_pack, &b->data[DSI_START_BYTE] );
694 block = dsi_pack.dsi_gi.nv_pck_lbn;
695 pack_len = dsi_pack.dsi_gi.vobu_ea;
696 next_vobu = block + ( dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
700 block >= d->next_vobu &&
701 ( block <= d->title_start + d->title_block_count ||
702 block <= d->title_end ) )
705 This looks like a valid VOBU, but actually we are
710 hb_log( "dvd: found VOBU at %d (b %d, l %d, n %d)",
711 d->next_vobu, block, pack_len, next_vobu );
715 d->pack_len = pack_len;
716 d->next_vobu = next_vobu;
720 /* Wasn't a valid VOBU, try next block */
724 hb_log( "dvd: looking for a VOBU (%d)", d->next_vobu );
730 hb_log( "dvd: couldn't find a VOBU after 1024 blocks" );
737 if( dsi_pack.vobu_sri.next_vobu == SRI_END_OF_CELL )
739 d->cell_cur = d->cell_next;
740 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
743 hb_log("DVD: End of Cell");
746 // Revert the cell overlap, and check for a chapter break
747 if( dsi_pack.vobu_sri.prev_vobu == SRI_END_OF_CELL )
749 hb_log("DVD: Beginning of Cell");
750 if( d->cell_overlap )
752 b->new_chap = hb_dvd_is_break( d );
759 if( DVDReadBlocks( d->file, d->block, 1, b->data ) != 1 )
761 hb_error( "reader: DVDReadBlocks failed (%d)", d->block );
772 /***********************************************************************
774 ***********************************************************************
775 * Returns in which chapter the next block to be read is.
776 * Chapter numbers start at 1.
777 **********************************************************************/
778 int hb_dvd_chapter( hb_dvd_t * d )
782 int nr_of_ptts = d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
785 for( i = nr_of_ptts - 1;
789 /* Get pgc for chapter (i+1) */
790 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
791 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
792 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
794 if( d->cell_cur - d->cell_overlap >= pgc->program_map[pgn-1] - 1 &&
795 d->cell_cur - d->cell_overlap <= pgc->nr_of_cells - 1 )
797 /* We are in this chapter */
806 /***********************************************************************
808 ***********************************************************************
809 * Returns 1 if the current block is a new chapter start
810 **********************************************************************/
811 int hb_dvd_is_break( hb_dvd_t * d )
815 int nr_of_ptts = d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
817 int cell, chapter_length, cell_end;
819 for( i = nr_of_ptts - 1;
823 /* Get pgc for chapter (i+1) */
824 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
825 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
826 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
827 cell = pgc->program_map[pgn-1] - 1;
829 if( cell <= d->cell_start )
832 // This must not match against the start cell.
833 if( pgc->cell_playback[cell].first_sector == d->block && cell != d->cell_start )
835 /* Check to see if we merged this chapter into the previous one... */
836 /* As a note, merging chapters is probably bad practice for this very reason */
839 if( i == nr_of_ptts - 1 )
841 cell_end = d->pgc->nr_of_cells;
845 cell_end = pgc->program_map[pgn] - 1;
848 for( j = cell; j < cell_end; j++ )
850 chapter_length += pgc->cell_playback[j].last_sector + 1 -
851 pgc->cell_playback[j].first_sector;
854 if( chapter_length >= 2048 )
856 hb_log("DVD: Chapter Break Cell Found");
857 /* We have a chapter break */
862 hb_log("DVD: Cell Found (%d)", chapter_length);
870 /***********************************************************************
872 ***********************************************************************
873 * Closes and frees everything
874 **********************************************************************/
875 void hb_dvd_close( hb_dvd_t ** _d )
885 DVDClose( d->reader );
892 /***********************************************************************
894 ***********************************************************************
895 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
896 * cell to be read when we will be done with cell_cur.
897 **********************************************************************/
898 static void FindNextCell( hb_dvd_t * d )
902 if( d->pgc->cell_playback[d->cell_cur].block_type ==
903 BLOCK_TYPE_ANGLE_BLOCK )
906 while( d->pgc->cell_playback[d->cell_cur+i].block_mode !=
907 BLOCK_MODE_LAST_CELL )
911 d->cell_next = d->cell_cur + i + 1;
915 d->cell_next = d->cell_cur + 1;
919 /***********************************************************************
921 ***********************************************************************
923 **********************************************************************/
924 static int dvdtime2msec(dvd_time_t * dt)
926 double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
927 double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
929 ms = (((dt->hour & 0xf0) >> 3) * 5 + (dt->hour & 0x0f)) * 3600000;
930 ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
931 ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
935 ms += ((dt->frame_u & 0x30) >> 3) * 5 +
936 (dt->frame_u & 0x0f) * 1000.0 / fps;