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;
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" ) );
250 snprintf( audio->lang_simple, sizeof( audio->lang_simple ), "%s",
251 lang_for_code( vts->vtsi_mat->vts_audio_attr[i].lang_code ) );
253 hb_log( "scan: id=%x, lang=%s", audio->id,
256 hb_list_add( title->list_audio, audio );
259 if( !hb_list_count( title->list_audio ) )
261 hb_log( "scan: ignoring title (no audio track)" );
265 memcpy( title->palette,
266 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette,
267 16 * sizeof( uint32_t ) );
269 /* Check for subtitles */
270 for( i = 0; i < vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
272 hb_subtitle_t * subtitle;
276 hb_log( "scan: checking subtitle %d", i + 1 );
279 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i];
281 if( !( spu_control & 0x80000000 ) )
283 hb_log( "scan: subtitle channel is not active" );
287 if( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
289 switch( vts->vtsi_mat->vts_video_attr.permitted_df )
292 position = spu_control & 0xFF;
295 position = ( spu_control >> 8 ) & 0xFF;
298 position = ( spu_control >> 16 ) & 0xFF;
303 position = ( spu_control >> 24 ) & 0x7F;
306 subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
307 subtitle->id = ( ( 0x20 + position ) << 8 ) | 0xbd;
308 snprintf( subtitle->lang, sizeof( subtitle->lang ), "%s",
309 lang_for_code( vts->vtsi_mat->vts_subp_attr[i].lang_code ) );
311 hb_log( "scan: id=%x, lang=%s", subtitle->id,
314 hb_list_add( title->list_subtitle, subtitle );
318 hb_log( "scan: title %d has %d chapters", t,
319 vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts );
321 i < vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts; i++ )
323 int pgc_id_next, pgn_next;
326 chapter = calloc( sizeof( hb_chapter_t ), 1 );
329 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgcn;
330 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgn;
331 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
334 chapter->cell_start = d->pgc->program_map[pgn-1] - 1;
335 chapter->block_start =
336 d->pgc->cell_playback[chapter->cell_start].first_sector;
339 if( i != vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts - 1 )
341 /* The cell before the starting cell of the next chapter,
343 pgc_id_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgcn;
344 pgn_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgn;
345 pgc_next = vts->vts_pgcit->pgci_srp[pgc_id_next-1].pgc;
346 chapter->cell_end = pgc_next->program_map[pgn_next-1] - 2;
347 if( chapter->cell_end < 0 )
356 /* ... the last cell of the title */
357 chapter->cell_end = title->cell_end;
360 d->pgc->cell_playback[chapter->cell_end].last_sector;
362 /* Block count, duration */
363 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
364 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
365 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
366 chapter->block_count = 0;
367 chapter->duration = 0;
369 d->cell_cur = chapter->cell_start;
370 while( d->cell_cur <= chapter->cell_end )
372 #define cp d->pgc->cell_playback[d->cell_cur]
373 chapter->block_count += cp.last_sector + 1 - cp.first_sector;
374 chapter->duration += 90LL * dvdtime2msec( &cp.playback_time );
377 d->cell_cur = d->cell_next;
380 if( chapter->block_count < 2048 && chapter->index > 1 )
382 hb_log( "scan: chapter %d too short (%d blocks, "
383 "cells=%d->%d), merging", chapter->index,
384 chapter->block_count, chapter->cell_start,
386 chapter_old = hb_list_item( title->list_chapter,
387 chapter->index - 2 );
388 chapter_old->cell_end = chapter->cell_end;
389 chapter_old->block_end = chapter->block_end;
390 chapter_old->block_count += chapter->block_count;
392 chapter = chapter_old;
396 hb_list_add( title->list_chapter, chapter );
401 /* The durations we get for chapters aren't precise. Scale them so
402 the total matches the title duration */
404 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
406 chapter = hb_list_item( title->list_chapter, i );
407 duration += chapter->duration;
409 duration_correction = (float) title->duration / (float) duration;
410 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
413 chapter = hb_list_item( title->list_chapter, i );
414 chapter->duration = duration_correction * chapter->duration;
415 seconds = ( chapter->duration + 45000 ) / 90000;
416 chapter->hours = seconds / 3600;
417 chapter->minutes = ( seconds % 3600 ) / 60;
418 chapter->seconds = seconds % 60;
420 hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %lld ms",
421 chapter->index, chapter->cell_start, chapter->cell_end,
422 chapter->block_start, chapter->block_end,
423 chapter->block_count, chapter->duration / 90 );
426 /* Get aspect. We don't get width/height/rate infos here as
427 they tend to be wrong */
428 switch( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
431 title->aspect = HB_ASPECT_BASE * 4 / 3;
434 title->aspect = HB_ASPECT_BASE * 16 / 9;
437 hb_log( "scan: unknown aspect" );
441 hb_log( "scan: aspect = %d", title->aspect );
443 /* This title is ok so far */
447 hb_list_close( &title->list_audio );
452 if( vts ) ifoClose( vts );
457 /***********************************************************************
459 ***********************************************************************
460 * Title and chapter start at 1
461 **********************************************************************/
462 int hb_dvd_start( hb_dvd_t * d, int title, int chapter )
467 /* Open the IFO and the VOBs for this title */
468 d->vts = d->vmg->tt_srpt->title[title-1].title_set_nr;
469 d->ttn = d->vmg->tt_srpt->title[title-1].vts_ttn;
470 if( !( d->ifo = ifoOpen( d->reader, d->vts ) ) )
472 hb_log( "dvd: ifoOpen failed for VTS %d", d->vts );
475 if( !( d->file = DVDOpenFile( d->reader, d->vts,
476 DVD_READ_TITLE_VOBS ) ) )
478 hb_log( "dvd: DVDOpenFile failed for VTS %d", d->vts );
482 /* Get title first and last blocks */
483 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgcn;
484 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgn;
485 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
486 d->cell_start = d->pgc->program_map[pgn - 1] - 1;
487 d->cell_end = d->pgc->nr_of_cells - 1;
488 d->title_start = d->pgc->cell_playback[d->cell_start].first_sector;
489 d->title_end = d->pgc->cell_playback[d->cell_end].last_sector;
492 d->title_block_count = 0;
493 for( i = d->cell_start; i <= d->cell_end; i++ )
495 d->title_block_count += d->pgc->cell_playback[i].last_sector + 1 -
496 d->pgc->cell_playback[i].first_sector;
499 /* Get pgc for the current chapter */
500 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgcn;
501 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgn;
502 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
504 /* Get the two first cells */
505 d->cell_cur = d->pgc->program_map[pgn-1] - 1;
508 d->block = d->pgc->cell_playback[d->cell_cur].first_sector;
509 d->next_vobu = d->block;
515 /***********************************************************************
517 ***********************************************************************
519 **********************************************************************/
520 void hb_dvd_stop( hb_dvd_t * d )
529 DVDCloseFile( d->file );
534 /***********************************************************************
536 ***********************************************************************
538 **********************************************************************/
539 int hb_dvd_seek( hb_dvd_t * d, float f )
544 count = f * d->title_block_count;
546 for( i = d->cell_start; i <= d->cell_end; i++ )
548 sizeCell = d->pgc->cell_playback[i].last_sector + 1 -
549 d->pgc->cell_playback[i].first_sector;
551 if( count < sizeCell )
556 /* Now let hb_dvd_read find the next VOBU */
557 d->next_vobu = d->pgc->cell_playback[i].first_sector + count;
565 if( i > d->cell_end )
573 /***********************************************************************
575 ***********************************************************************
577 **********************************************************************/
578 int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
590 int block, pack_len, next_vobu;
592 if( DVDReadBlocks( d->file, d->next_vobu, 1, b->data ) != 1 )
594 hb_log( "dvd: DVDReadBlocks failed (%d)", d->next_vobu );
598 navRead_DSI( &dsi_pack, &b->data[DSI_START_BYTE] );
600 block = dsi_pack.dsi_gi.nv_pck_lbn;
601 pack_len = dsi_pack.dsi_gi.vobu_ea;
602 next_vobu = block + ( dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
606 block >= d->next_vobu &&
607 ( block <= d->title_start + d->title_block_count ||
608 block <= d->title_end ) )
611 This looks like a valid VOBU, but actually we are
616 hb_log( "dvd: found VOBU at %d (b %d, l %d, n %d)",
617 d->next_vobu, block, pack_len, next_vobu );
621 d->pack_len = pack_len;
622 d->next_vobu = next_vobu;
626 /* Wasn't a valid VOBU, try next block */
630 hb_log( "dvd: looking for a VOBU (%d)", d->next_vobu );
636 hb_log( "dvd: couldn't find a VOBU after 1024 blocks" );
643 if( dsi_pack.vobu_sri.next_vobu == SRI_END_OF_CELL )
645 d->cell_cur = d->cell_next;
646 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
652 if( DVDReadBlocks( d->file, d->block, 1, b->data ) != 1 )
654 hb_log( "reader: DVDReadBlocks failed (%d)", d->block );
665 /***********************************************************************
667 ***********************************************************************
668 * Returns in which chapter the next block to be read is.
669 * Chapter numbers start at 1.
670 **********************************************************************/
671 int hb_dvd_chapter( hb_dvd_t * d )
678 i < d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
681 /* Get pgc for chapter (i+1) */
682 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
683 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
684 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
686 if( d->cell_cur >= pgc->program_map[pgn-1] - 1 &&
687 d->cell_cur <= pgc->nr_of_cells - 1 )
689 /* We are in this chapter */
698 /***********************************************************************
700 ***********************************************************************
701 * Closes and frees everything
702 **********************************************************************/
703 void hb_dvd_close( hb_dvd_t ** _d )
713 DVDClose( d->reader );
720 /***********************************************************************
722 ***********************************************************************
723 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
724 * cell to be read when we will be done with cell_cur.
725 **********************************************************************/
726 static void FindNextCell( hb_dvd_t * d )
730 if( d->pgc->cell_playback[d->cell_cur].block_type ==
731 BLOCK_TYPE_ANGLE_BLOCK )
734 while( d->pgc->cell_playback[d->cell_cur+i].block_mode !=
735 BLOCK_MODE_LAST_CELL )
739 d->cell_next = d->cell_cur + i + 1;
743 d->cell_next = d->cell_cur + 1;
747 /***********************************************************************
749 ***********************************************************************
751 **********************************************************************/
752 static int dvdtime2msec(dvd_time_t * dt)
754 double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
755 double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
757 ms = (((dt->hour & 0xf0) >> 3) * 5 + (dt->hour & 0x0f)) * 3600000;
758 ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
759 ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
763 ms += ((dt->frame_u & 0x30) >> 3) * 5 +
764 (dt->frame_u & 0x0f) * 1000.0 / fps;