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 char * hb_dvd_name( char * path )
46 static char name[1024];
47 unsigned char unused[1024];
48 dvd_reader_t * reader;
50 reader = DVDOpen( path );
56 if( DVDUDFVolumeInfo( reader, name, sizeof( name ),
57 unused, sizeof( unused ) ) )
67 /***********************************************************************
69 ***********************************************************************
71 **********************************************************************/
72 hb_dvd_t * hb_dvd_init( char * path )
76 d = calloc( sizeof( hb_dvd_t ), 1 );
79 if( !( d->reader = DVDOpen( path ) ) )
81 hb_log( "dvd: DVDOpen failed (%s)", path );
86 if( !( d->vmg = ifoOpen( d->reader, 0 ) ) )
88 hb_log( "dvd: ifoOpen failed" );
92 d->path = strdup( path );
97 if( d->vmg ) ifoClose( d->vmg );
98 if( d->reader ) DVDClose( d->reader );
103 /***********************************************************************
105 **********************************************************************/
106 int hb_dvd_title_count( hb_dvd_t * d )
108 return d->vmg->tt_srpt->nr_of_srpts;
111 /***********************************************************************
113 **********************************************************************/
114 hb_title_t * hb_dvd_title_scan( hb_dvd_t * d, int t )
118 ifo_handle_t * vts = NULL;
120 hb_chapter_t * chapter, * chapter_old;
123 float duration_correction;
124 unsigned char unused[1024];
126 hb_log( "scan: scanning title %d", t );
128 title = hb_title_init( d->path, t );
129 if( DVDUDFVolumeInfo( d->reader, title->name, sizeof( title->name ),
130 unused, sizeof( unused ) ) )
132 char * p_cur, * p_last = d->path;
133 for( p_cur = d->path; *p_cur; p_cur++ )
135 if( p_cur[0] == '/' && p_cur[1] )
140 snprintf( title->name, sizeof( title->name ), "%s", p_last );
143 /* VTS which our title is in */
144 title->vts = d->vmg->tt_srpt->title[t-1].title_set_nr;
146 hb_log( "scan: opening IFO for VTS %d", title->vts );
147 if( !( vts = ifoOpen( d->reader, title->vts ) ) )
149 hb_log( "scan: ifoOpen failed" );
153 /* Position of the title in the VTS */
154 title->ttn = d->vmg->tt_srpt->title[t-1].vts_ttn;
157 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
158 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
159 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
162 title->cell_start = d->pgc->program_map[pgn-1] - 1;
163 title->block_start = d->pgc->cell_playback[title->cell_start].first_sector;
166 title->cell_end = d->pgc->nr_of_cells - 1;
167 title->block_end = d->pgc->cell_playback[title->cell_end].last_sector;
170 title->block_count = 0;
171 d->cell_cur = title->cell_start;
172 while( d->cell_cur <= title->cell_end )
174 #define cp d->pgc->cell_playback[d->cell_cur]
175 title->block_count += cp.last_sector + 1 - cp.first_sector;
178 d->cell_cur = d->cell_next;
181 hb_log( "scan: vts=%d, ttn=%d, cells=%d->%d, blocks=%d->%d, "
182 "%d blocks", title->vts, title->ttn, title->cell_start,
183 title->cell_end, title->block_start, title->block_end,
184 title->block_count );
186 if( title->block_count < 2048 )
188 hb_log( "scan: title too short (%d blocks), ignoring",
189 title->block_count );
195 title->duration = 90LL * dvdtime2msec( &d->pgc->playback_time );
196 title->hours = title->duration / 90000 / 3600;
197 title->minutes = ( ( title->duration / 90000 ) % 3600 ) / 60;
198 title->seconds = ( title->duration / 90000 ) % 60;
199 hb_log( "scan: duration is %02d:%02d:%02d (%lld ms)",
200 title->hours, title->minutes, title->seconds,
201 title->duration / 90 );
203 /* Discard titles under 10 seconds */
204 if( !( title->hours | title->minutes ) && title->seconds < 10 )
206 hb_log( "scan: ignoring title (too short)" );
210 /* Detect languages */
211 for( i = 0; i < vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
213 hb_audio_t * audio, * audio_tmp;
214 int audio_format, lang_code, audio_control,
217 hb_log( "scan: checking audio %d", i + 1 );
219 audio = calloc( sizeof( hb_audio_t ), 1 );
221 audio_format = vts->vtsi_mat->vts_audio_attr[i].audio_format;
222 lang_code = vts->vtsi_mat->vts_audio_attr[i].lang_code;
224 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i];
226 if( !( audio_control & 0x8000 ) )
228 hb_log( "scan: audio channel is not active" );
233 position = ( audio_control & 0x7F00 ) >> 8;
235 switch( audio_format )
238 audio->id = ( ( 0x80 + position ) << 8 ) | 0xbd;
239 audio->codec = HB_ACODEC_AC3;
244 audio->id = 0xc0 + position;
245 audio->codec = HB_ACODEC_MPGA;
249 audio->id = ( ( 0xa0 + position ) << 8 ) | 0xbd;
250 audio->codec = HB_ACODEC_LPCM;
256 hb_log( "scan: unknown audio codec (%x)",
265 /* Check for duplicate tracks */
267 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
269 audio_tmp = hb_list_item( title->list_audio, j );
270 if( audio->id == audio_tmp->id )
278 hb_log( "scan: duplicate audio track" );
283 snprintf( audio->lang, sizeof( audio->lang ), "%s (%s)",
284 lang_for_code( vts->vtsi_mat->vts_audio_attr[i].lang_code ),
285 audio->codec == HB_ACODEC_AC3 ? "AC3" : ( audio->codec ==
286 HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) );
287 snprintf( audio->lang_simple, sizeof( audio->lang_simple ), "%s",
288 lang_for_code( vts->vtsi_mat->vts_audio_attr[i].lang_code ) );
290 hb_log( "scan: id=%x, lang=%s", audio->id,
293 hb_list_add( title->list_audio, audio );
296 if( !hb_list_count( title->list_audio ) )
298 hb_log( "scan: ignoring title (no audio track)" );
302 memcpy( title->palette,
303 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette,
304 16 * sizeof( uint32_t ) );
306 /* Check for subtitles */
307 for( i = 0; i < vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
309 hb_subtitle_t * subtitle;
313 hb_log( "scan: checking subtitle %d", i + 1 );
316 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i];
318 if( !( spu_control & 0x80000000 ) )
320 hb_log( "scan: subtitle channel is not active" );
324 if( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
326 switch( vts->vtsi_mat->vts_video_attr.permitted_df )
329 position = spu_control & 0xFF;
332 position = ( spu_control >> 8 ) & 0xFF;
335 position = ( spu_control >> 16 ) & 0xFF;
340 position = ( spu_control >> 24 ) & 0x7F;
343 subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
344 subtitle->id = ( ( 0x20 + position ) << 8 ) | 0xbd;
345 snprintf( subtitle->lang, sizeof( subtitle->lang ), "%s",
346 lang_for_code( vts->vtsi_mat->vts_subp_attr[i].lang_code ) );
348 hb_log( "scan: id=%x, lang=%s", subtitle->id,
351 hb_list_add( title->list_subtitle, subtitle );
355 hb_log( "scan: title %d has %d chapters", t,
356 vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts );
358 i < vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts; i++ )
360 int pgc_id_next, pgn_next;
363 chapter = calloc( sizeof( hb_chapter_t ), 1 );
366 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgcn;
367 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgn;
368 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
371 chapter->cell_start = d->pgc->program_map[pgn-1] - 1;
372 chapter->block_start =
373 d->pgc->cell_playback[chapter->cell_start].first_sector;
376 if( i != vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts - 1 )
378 /* The cell before the starting cell of the next chapter,
380 pgc_id_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgcn;
381 pgn_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgn;
382 pgc_next = vts->vts_pgcit->pgci_srp[pgc_id_next-1].pgc;
383 chapter->cell_end = pgc_next->program_map[pgn_next-1] - 2;
384 if( chapter->cell_end < 0 )
393 /* ... the last cell of the title */
394 chapter->cell_end = title->cell_end;
397 d->pgc->cell_playback[chapter->cell_end].last_sector;
399 /* Block count, duration */
400 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
401 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
402 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
403 chapter->block_count = 0;
404 chapter->duration = 0;
406 d->cell_cur = chapter->cell_start;
407 while( d->cell_cur <= chapter->cell_end )
409 #define cp d->pgc->cell_playback[d->cell_cur]
410 chapter->block_count += cp.last_sector + 1 - cp.first_sector;
411 chapter->duration += 90LL * dvdtime2msec( &cp.playback_time );
414 d->cell_cur = d->cell_next;
417 if( chapter->block_count < 2048 && chapter->index > 1 )
419 hb_log( "scan: chapter %d too short (%d blocks, "
420 "cells=%d->%d), merging", chapter->index,
421 chapter->block_count, chapter->cell_start,
423 chapter_old = hb_list_item( title->list_chapter,
424 chapter->index - 2 );
425 chapter_old->cell_end = chapter->cell_end;
426 chapter_old->block_end = chapter->block_end;
427 chapter_old->block_count += chapter->block_count;
429 chapter = chapter_old;
433 hb_list_add( title->list_chapter, chapter );
438 /* The durations we get for chapters aren't precise. Scale them so
439 the total matches the title duration */
441 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
443 chapter = hb_list_item( title->list_chapter, i );
444 duration += chapter->duration;
446 duration_correction = (float) title->duration / (float) duration;
447 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
450 chapter = hb_list_item( title->list_chapter, i );
451 chapter->duration = duration_correction * chapter->duration;
452 seconds = ( chapter->duration + 45000 ) / 90000;
453 chapter->hours = seconds / 3600;
454 chapter->minutes = ( seconds % 3600 ) / 60;
455 chapter->seconds = seconds % 60;
457 hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %lld ms",
458 chapter->index, chapter->cell_start, chapter->cell_end,
459 chapter->block_start, chapter->block_end,
460 chapter->block_count, chapter->duration / 90 );
463 /* Get aspect. We don't get width/height/rate infos here as
464 they tend to be wrong */
465 switch( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
468 title->aspect = HB_ASPECT_BASE * 4 / 3;
471 title->aspect = HB_ASPECT_BASE * 16 / 9;
474 hb_log( "scan: unknown aspect" );
478 hb_log( "scan: aspect = %d", title->aspect );
480 /* This title is ok so far */
484 hb_list_close( &title->list_audio );
489 if( vts ) ifoClose( vts );
494 /***********************************************************************
496 ***********************************************************************
497 * Title and chapter start at 1
498 **********************************************************************/
499 int hb_dvd_start( hb_dvd_t * d, int title, int chapter )
504 /* Open the IFO and the VOBs for this title */
505 d->vts = d->vmg->tt_srpt->title[title-1].title_set_nr;
506 d->ttn = d->vmg->tt_srpt->title[title-1].vts_ttn;
507 if( !( d->ifo = ifoOpen( d->reader, d->vts ) ) )
509 hb_log( "dvd: ifoOpen failed for VTS %d", d->vts );
512 if( !( d->file = DVDOpenFile( d->reader, d->vts,
513 DVD_READ_TITLE_VOBS ) ) )
515 hb_log( "dvd: DVDOpenFile failed for VTS %d", d->vts );
519 /* Get title first and last blocks */
520 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgcn;
521 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgn;
522 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
523 d->cell_start = d->pgc->program_map[pgn - 1] - 1;
524 d->cell_end = d->pgc->nr_of_cells - 1;
525 d->title_start = d->pgc->cell_playback[d->cell_start].first_sector;
526 d->title_end = d->pgc->cell_playback[d->cell_end].last_sector;
529 d->title_block_count = 0;
530 for( i = d->cell_start; i <= d->cell_end; i++ )
532 d->title_block_count += d->pgc->cell_playback[i].last_sector + 1 -
533 d->pgc->cell_playback[i].first_sector;
536 /* Get pgc for the current chapter */
537 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgcn;
538 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgn;
539 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
541 /* Get the two first cells */
542 d->cell_cur = d->pgc->program_map[pgn-1] - 1;
545 d->block = d->pgc->cell_playback[d->cell_cur].first_sector;
546 d->next_vobu = d->block;
552 /***********************************************************************
554 ***********************************************************************
556 **********************************************************************/
557 void hb_dvd_stop( hb_dvd_t * d )
566 DVDCloseFile( d->file );
571 /***********************************************************************
573 ***********************************************************************
575 **********************************************************************/
576 int hb_dvd_seek( hb_dvd_t * d, float f )
581 count = f * d->title_block_count;
583 for( i = d->cell_start; i <= d->cell_end; i++ )
585 sizeCell = d->pgc->cell_playback[i].last_sector + 1 -
586 d->pgc->cell_playback[i].first_sector;
588 if( count < sizeCell )
593 /* Now let hb_dvd_read find the next VOBU */
594 d->next_vobu = d->pgc->cell_playback[i].first_sector + count;
602 if( i > d->cell_end )
611 /***********************************************************************
613 ***********************************************************************
614 * Pretty much directly lifted from libdvdread's play_title function.
615 **********************************************************************/
616 int is_nav_pack( unsigned char *buf )
618 return ( buf[41] == 0xbf && buf[1027] == 0xbf );
622 /***********************************************************************
624 ***********************************************************************
626 **********************************************************************/
627 int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
639 int block, pack_len, next_vobu;
641 if( DVDReadBlocks( d->file, d->next_vobu, 1, b->data ) != 1 )
643 hb_log( "dvd: DVDReadBlocks failed (%d)", d->next_vobu );
647 if ( !is_nav_pack( b->data ) ) {
652 navRead_DSI( &dsi_pack, &b->data[DSI_START_BYTE] );
654 block = dsi_pack.dsi_gi.nv_pck_lbn;
655 pack_len = dsi_pack.dsi_gi.vobu_ea;
656 next_vobu = block + ( dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
660 block >= d->next_vobu &&
661 ( block <= d->title_start + d->title_block_count ||
662 block <= d->title_end ) )
665 This looks like a valid VOBU, but actually we are
670 hb_log( "dvd: found VOBU at %d (b %d, l %d, n %d)",
671 d->next_vobu, block, pack_len, next_vobu );
675 d->pack_len = pack_len;
676 d->next_vobu = next_vobu;
680 /* Wasn't a valid VOBU, try next block */
684 hb_log( "dvd: looking for a VOBU (%d)", d->next_vobu );
690 hb_log( "dvd: couldn't find a VOBU after 1024 blocks" );
697 if( dsi_pack.vobu_sri.next_vobu == SRI_END_OF_CELL )
699 d->cell_cur = d->cell_next;
700 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
706 if( DVDReadBlocks( d->file, d->block, 1, b->data ) != 1 )
708 hb_log( "reader: DVDReadBlocks failed (%d)", d->block );
719 /***********************************************************************
721 ***********************************************************************
722 * Returns in which chapter the next block to be read is.
723 * Chapter numbers start at 1.
724 **********************************************************************/
725 int hb_dvd_chapter( hb_dvd_t * d )
732 i < d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
735 /* Get pgc for chapter (i+1) */
736 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
737 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
738 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
740 if( d->cell_cur >= pgc->program_map[pgn-1] - 1 &&
741 d->cell_cur <= pgc->nr_of_cells - 1 )
743 /* We are in this chapter */
752 /***********************************************************************
754 ***********************************************************************
755 * Closes and frees everything
756 **********************************************************************/
757 void hb_dvd_close( hb_dvd_t ** _d )
767 DVDClose( d->reader );
774 /***********************************************************************
776 ***********************************************************************
777 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
778 * cell to be read when we will be done with cell_cur.
779 **********************************************************************/
780 static void FindNextCell( hb_dvd_t * d )
784 if( d->pgc->cell_playback[d->cell_cur].block_type ==
785 BLOCK_TYPE_ANGLE_BLOCK )
788 while( d->pgc->cell_playback[d->cell_cur+i].block_mode !=
789 BLOCK_MODE_LAST_CELL )
793 d->cell_next = d->cell_cur + i + 1;
797 d->cell_next = d->cell_cur + 1;
801 /***********************************************************************
803 ***********************************************************************
805 **********************************************************************/
806 static int dvdtime2msec(dvd_time_t * dt)
808 double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
809 double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
811 ms = (((dt->hour & 0xf0) >> 3) * 5 + (dt->hour & 0x0f)) * 3600000;
812 ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
813 ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
817 ms += ((dt->frame_u & 0x30) >> 3) * 5 +
818 (dt->frame_u & 0x0f) * 1000.0 / fps;