1 /* $Id: dvd.c,v 1.11 2005/11/04 15:30:47 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;
38 /***********************************************************************
40 **********************************************************************/
41 static void FindNextCell( hb_dvd_t * );
42 static int dvdtime2msec( dvd_time_t * );
44 /***********************************************************************
46 ***********************************************************************
48 **********************************************************************/
49 hb_dvd_t * hb_dvd_init( char * path )
53 d = calloc( sizeof( hb_dvd_t ), 1 );
56 if( !( d->reader = DVDOpen( path ) ) )
58 hb_log( "dvd: DVDOpen failed (%s)", path );
63 if( !( d->vmg = ifoOpen( d->reader, 0 ) ) )
65 hb_log( "dvd: ifoOpen failed" );
69 d->path = strdup( path );
74 if( d->vmg ) ifoClose( d->vmg );
75 if( d->reader ) DVDClose( d->reader );
80 /***********************************************************************
82 **********************************************************************/
83 int hb_dvd_title_count( hb_dvd_t * d )
85 return d->vmg->tt_srpt->nr_of_srpts;
88 /***********************************************************************
90 **********************************************************************/
91 hb_title_t * hb_dvd_title_scan( hb_dvd_t * d, int t )
95 ifo_handle_t * vts = NULL;
97 hb_chapter_t * chapter, * chapter_old;
100 float duration_correction;
102 title = hb_title_init( d->path, t );
104 hb_log( "scan: scanning title %d", t );
106 /* VTS which our title is in */
107 title->vts = d->vmg->tt_srpt->title[t-1].title_set_nr;
109 hb_log( "scan: opening IFO for VTS %d", title->vts );
110 if( !( vts = ifoOpen( d->reader, title->vts ) ) )
112 hb_log( "scan: ifoOpen failed" );
116 /* Position of the title in the VTS */
117 title->ttn = d->vmg->tt_srpt->title[t-1].vts_ttn;
120 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
121 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
122 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
125 title->cell_start = d->pgc->program_map[pgn-1] - 1;
126 title->block_start = d->pgc->cell_playback[title->cell_start].first_sector;
129 title->cell_end = d->pgc->nr_of_cells - 1;
130 title->block_end = d->pgc->cell_playback[title->cell_end].last_sector;
133 title->block_count = 0;
134 d->cell_cur = title->cell_start;
135 while( d->cell_cur <= title->cell_end )
137 #define cp d->pgc->cell_playback[d->cell_cur]
138 title->block_count += cp.last_sector + 1 - cp.first_sector;
141 d->cell_cur = d->cell_next;
144 hb_log( "scan: vts=%d, ttn=%d, cells=%d->%d, blocks=%d->%d, "
145 "%d blocks", title->vts, title->ttn, title->cell_start,
146 title->cell_end, title->block_start, title->block_end,
147 title->block_count );
149 if( title->block_count < 2048 )
151 hb_log( "scan: title too short (%d blocks), ignoring",
152 title->block_count );
158 title->duration = 90LL * dvdtime2msec( &d->pgc->playback_time );
159 title->hours = title->duration / 90000 / 3600;
160 title->minutes = ( ( title->duration / 90000 ) % 3600 ) / 60;
161 title->seconds = ( title->duration / 90000 ) % 60;
162 hb_log( "scan: duration is %02d:%02d:%02d (%lld ms)",
163 title->hours, title->minutes, title->seconds,
164 title->duration / 90 );
166 /* Discard titles under 10 seconds */
167 if( !( title->hours | title->minutes ) && title->seconds < 10 )
169 hb_log( "scan: ignoring title (too short)" );
173 /* Detect languages */
174 for( i = 0; i < vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
176 hb_audio_t * audio, * audio_tmp;
177 int audio_format, lang_code, audio_control,
180 hb_log( "scan: checking audio %d", i + 1 );
182 audio = calloc( sizeof( hb_audio_t ), 1 );
184 audio_format = vts->vtsi_mat->vts_audio_attr[i].audio_format;
185 lang_code = vts->vtsi_mat->vts_audio_attr[i].lang_code;
187 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i];
189 if( !( audio_control & 0x8000 ) )
191 hb_log( "scan: audio channel is not active" );
196 position = ( audio_control & 0x7F00 ) >> 8;
198 switch( audio_format )
201 audio->id = ( ( 0x80 + position ) << 8 ) | 0xbd;
202 audio->codec = HB_ACODEC_AC3;
207 audio->id = 0xc0 + position;
208 audio->codec = HB_ACODEC_MPGA;
212 audio->id = ( ( 0xa0 + position ) << 8 ) | 0xbd;
213 audio->codec = HB_ACODEC_LPCM;
219 hb_log( "scan: unknown audio codec (%x)",
228 /* Check for duplicate tracks */
230 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
232 audio_tmp = hb_list_item( title->list_audio, j );
233 if( audio->id == audio_tmp->id )
241 hb_log( "scan: duplicate audio track" );
246 snprintf( audio->lang, sizeof( audio->lang ), "%s (%s)",
247 lang_for_code( vts->vtsi_mat->vts_audio_attr[i].lang_code ),
248 audio->codec == HB_ACODEC_AC3 ? "AC3" : ( audio->codec ==
249 HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) );
251 hb_log( "scan: id=%x, lang=%s", audio->id,
254 hb_list_add( title->list_audio, audio );
257 if( !hb_list_count( title->list_audio ) )
259 hb_log( "scan: ignoring title (no audio track)" );
263 memcpy( title->palette,
264 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette,
265 16 * sizeof( uint32_t ) );
267 /* Check for subtitles */
268 for( i = 0; i < vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
270 hb_subtitle_t * subtitle;
274 hb_log( "scan: checking subtitle %d", i + 1 );
277 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i];
279 if( !( spu_control & 0x80000000 ) )
281 hb_log( "scan: subtitle channel is not active" );
285 if( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
287 switch( vts->vtsi_mat->vts_video_attr.permitted_df )
290 position = spu_control & 0xFF;
293 position = ( spu_control >> 8 ) & 0xFF;
296 position = ( spu_control >> 16 ) & 0xFF;
301 position = ( spu_control >> 24 ) & 0x7F;
304 subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
305 subtitle->id = ( ( 0x20 + position ) << 8 ) | 0xbd;
306 snprintf( subtitle->lang, sizeof( subtitle->lang ), "%s",
307 lang_for_code( vts->vtsi_mat->vts_subp_attr[i].lang_code ) );
309 hb_log( "scan: id=%x, lang=%s", subtitle->id,
312 hb_list_add( title->list_subtitle, subtitle );
316 hb_log( "scan: title %d has %d chapters", t,
317 vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts );
319 i < vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts; i++ )
321 int pgc_id_next, pgn_next;
324 chapter = calloc( sizeof( hb_chapter_t ), 1 );
327 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgcn;
328 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgn;
329 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
332 chapter->cell_start = d->pgc->program_map[pgn-1] - 1;
333 chapter->block_start =
334 d->pgc->cell_playback[chapter->cell_start].first_sector;
337 if( i != vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts - 1 )
339 /* The cell before the starting cell of the next chapter,
341 pgc_id_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgcn;
342 pgn_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgn;
343 pgc_next = vts->vts_pgcit->pgci_srp[pgc_id_next-1].pgc;
344 chapter->cell_end = pgc_next->program_map[pgn_next-1] - 2;
345 if( chapter->cell_end < 0 )
354 /* ... the last cell of the title */
355 chapter->cell_end = title->cell_end;
358 d->pgc->cell_playback[chapter->cell_end].last_sector;
360 /* Block count, duration */
361 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
362 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
363 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
364 chapter->block_count = 0;
365 chapter->duration = 0;
367 d->cell_cur = chapter->cell_start;
368 while( d->cell_cur <= chapter->cell_end )
370 #define cp d->pgc->cell_playback[d->cell_cur]
371 chapter->block_count += cp.last_sector + 1 - cp.first_sector;
372 chapter->duration += 90LL * dvdtime2msec( &cp.playback_time );
375 d->cell_cur = d->cell_next;
378 if( chapter->block_count < 2048 && chapter->index > 1 )
380 hb_log( "scan: chapter %d too short (%d blocks, "
381 "cells=%d->%d), merging", chapter->index,
382 chapter->block_count, chapter->cell_start,
384 chapter_old = hb_list_item( title->list_chapter,
385 chapter->index - 2 );
386 chapter_old->cell_end = chapter->cell_end;
387 chapter_old->block_end = chapter->block_end;
388 chapter_old->block_count += chapter->block_count;
390 chapter = chapter_old;
394 hb_list_add( title->list_chapter, chapter );
399 /* The durations we get for chapters aren't precise. Scale them so
400 the total matches the title duration */
402 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
404 chapter = hb_list_item( title->list_chapter, i );
405 duration += chapter->duration;
407 duration_correction = (float) title->duration / (float) duration;
408 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
411 chapter = hb_list_item( title->list_chapter, i );
412 chapter->duration = duration_correction * chapter->duration;
413 seconds = ( chapter->duration + 45000 ) / 90000;
414 chapter->hours = seconds / 3600;
415 chapter->minutes = ( seconds % 3600 ) / 60;
416 chapter->seconds = seconds % 60;
418 hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %lld ms",
419 chapter->index, chapter->cell_start, chapter->cell_end,
420 chapter->block_start, chapter->block_end,
421 chapter->block_count, chapter->duration / 90 );
424 /* Get aspect. We don't get width/height/rate infos here as
425 they tend to be wrong */
426 switch( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
429 title->aspect = HB_ASPECT_BASE * 4 / 3;
432 title->aspect = HB_ASPECT_BASE * 16 / 9;
435 hb_log( "scan: unknown aspect" );
439 hb_log( "scan: aspect = %d", title->aspect );
441 /* This title is ok so far */
445 hb_list_close( &title->list_audio );
450 if( vts ) ifoClose( vts );
455 /***********************************************************************
457 ***********************************************************************
458 * Title and chapter start at 1
459 **********************************************************************/
460 int hb_dvd_start( hb_dvd_t * d, int title, int chapter )
465 /* Open the IFO and the VOBs for this title */
466 d->vts = d->vmg->tt_srpt->title[title-1].title_set_nr;
467 d->ttn = d->vmg->tt_srpt->title[title-1].vts_ttn;
468 if( !( d->ifo = ifoOpen( d->reader, d->vts ) ) )
470 hb_log( "dvd: ifoOpen failed for VTS %d", d->vts );
473 if( !( d->file = DVDOpenFile( d->reader, d->vts,
474 DVD_READ_TITLE_VOBS ) ) )
476 hb_log( "dvd: DVDOpenFile failed for VTS %d", d->vts );
480 /* Get title first and last blocks */
481 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgcn;
482 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgn;
483 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
484 d->cell_start = d->pgc->program_map[pgn - 1] - 1;
485 d->cell_end = d->pgc->nr_of_cells - 1;
486 d->title_start = d->pgc->cell_playback[d->cell_start].first_sector;
487 d->title_end = d->pgc->cell_playback[d->cell_end].last_sector;
490 d->title_block_count = 0;
491 for( i = d->cell_start; i <= d->cell_end; i++ )
493 d->title_block_count += d->pgc->cell_playback[i].last_sector + 1 -
494 d->pgc->cell_playback[i].first_sector;
497 /* Get pgc for the current chapter */
498 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgcn;
499 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgn;
500 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
502 /* Get the two first cells */
503 d->cell_cur = d->pgc->program_map[pgn-1] - 1;
506 d->block = d->pgc->cell_playback[d->cell_cur].first_sector;
507 d->next_vobu = d->block;
513 /***********************************************************************
515 ***********************************************************************
517 **********************************************************************/
518 int hb_dvd_seek( hb_dvd_t * d, float f )
522 target = d->title_start + (int) ( f * d->title_block_count );
524 /* Find the cell we shall start in */
525 d->cell_cur = d->cell_start;
529 if( target < d->pgc->cell_playback[d->cell_cur].last_sector )
533 d->cell_cur = d->cell_next;
537 /* Now let hb_dvd_read find the next VOBU */
538 d->next_vobu = target;
544 /***********************************************************************
546 ***********************************************************************
548 **********************************************************************/
549 int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
561 int block, pack_len, next_vobu;
563 if( DVDReadBlocks( d->file, d->next_vobu, 1, b->data ) != 1 )
565 hb_log( "dvd: DVDReadBlocks failed (%d)", d->next_vobu );
569 navRead_DSI( &dsi_pack, &b->data[DSI_START_BYTE] );
571 block = dsi_pack.dsi_gi.nv_pck_lbn;
572 pack_len = dsi_pack.dsi_gi.vobu_ea;
573 next_vobu = block + ( dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
577 block >= d->next_vobu &&
578 ( block <= d->title_start + d->title_block_count ||
579 block <= d->title_end ) )
582 This looks like a valid VOBU, but actually we are
587 hb_log( "dvd: found VOBU at %d (b %d, l %d, n %d)",
588 d->next_vobu, block, pack_len, next_vobu );
592 d->pack_len = pack_len;
593 d->next_vobu = next_vobu;
597 /* Wasn't a valid VOBU, try next block */
601 hb_log( "dvd: looking for a VOBU (%d)", d->next_vobu );
607 hb_log( "dvd: couldn't find a VOBU after 1024 blocks" );
614 if( dsi_pack.vobu_sri.next_vobu == SRI_END_OF_CELL )
616 d->cell_cur = d->cell_next;
617 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
623 if( DVDReadBlocks( d->file, d->block, 1, b->data ) != 1 )
625 hb_log( "reader: DVDReadBlocks failed (%d)", d->block );
636 /***********************************************************************
638 ***********************************************************************
639 * Returns in which chapter the next block to be read is.
640 * Chapter numbers start at 1.
641 **********************************************************************/
642 int hb_dvd_chapter( hb_dvd_t * d )
649 i < d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
652 /* Get pgc for chapter (i+1) */
653 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
654 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
655 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
657 if( d->cell_cur >= pgc->program_map[pgn-1] - 1 &&
658 d->cell_cur <= pgc->nr_of_cells - 1 )
660 /* We are in this chapter */
669 /***********************************************************************
671 ***********************************************************************
672 * Closes and frees everything
673 **********************************************************************/
674 void hb_dvd_close( hb_dvd_t ** _d )
678 if( d->ifo ) ifoClose( d->ifo );
679 if( d->vmg ) ifoClose( d->vmg );
680 if( d->file ) DVDCloseFile( d->file );
681 if( d->reader ) DVDClose( d->reader );
687 /***********************************************************************
689 ***********************************************************************
690 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
691 * cell to be read when we will be done with cell_cur.
692 **********************************************************************/
693 static void FindNextCell( hb_dvd_t * d )
697 if( d->pgc->cell_playback[d->cell_cur].block_type ==
698 BLOCK_TYPE_ANGLE_BLOCK )
701 while( d->pgc->cell_playback[d->cell_cur+i].block_mode !=
702 BLOCK_MODE_LAST_CELL )
706 d->cell_next = d->cell_cur + i + 1;
710 d->cell_next = d->cell_cur + 1;
714 /***********************************************************************
716 ***********************************************************************
718 **********************************************************************/
719 static int dvdtime2msec(dvd_time_t * dt)
721 double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
722 double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
724 ms = (((dt->hour & 0xf0) >> 3) * 5 + (dt->hour & 0x0f)) * 3600000;
725 ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
726 ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
730 ms += ((dt->frame_u & 0x30) >> 3) * 5 +
731 (dt->frame_u & 0x0f) * 1000.0 / fps;