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 title = hb_title_init( d->path, t );
127 if( DVDUDFVolumeInfo( d->reader, title->name, sizeof( title->name ),
128 unused, sizeof( unused ) ) )
133 hb_log( "scan: scanning title %d", t );
135 /* VTS which our title is in */
136 title->vts = d->vmg->tt_srpt->title[t-1].title_set_nr;
138 hb_log( "scan: opening IFO for VTS %d", title->vts );
139 if( !( vts = ifoOpen( d->reader, title->vts ) ) )
141 hb_log( "scan: ifoOpen failed" );
145 /* Position of the title in the VTS */
146 title->ttn = d->vmg->tt_srpt->title[t-1].vts_ttn;
149 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
150 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
151 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
154 title->cell_start = d->pgc->program_map[pgn-1] - 1;
155 title->block_start = d->pgc->cell_playback[title->cell_start].first_sector;
158 title->cell_end = d->pgc->nr_of_cells - 1;
159 title->block_end = d->pgc->cell_playback[title->cell_end].last_sector;
162 title->block_count = 0;
163 d->cell_cur = title->cell_start;
164 while( d->cell_cur <= title->cell_end )
166 #define cp d->pgc->cell_playback[d->cell_cur]
167 title->block_count += cp.last_sector + 1 - cp.first_sector;
170 d->cell_cur = d->cell_next;
173 hb_log( "scan: vts=%d, ttn=%d, cells=%d->%d, blocks=%d->%d, "
174 "%d blocks", title->vts, title->ttn, title->cell_start,
175 title->cell_end, title->block_start, title->block_end,
176 title->block_count );
178 if( title->block_count < 2048 )
180 hb_log( "scan: title too short (%d blocks), ignoring",
181 title->block_count );
187 title->duration = 90LL * dvdtime2msec( &d->pgc->playback_time );
188 title->hours = title->duration / 90000 / 3600;
189 title->minutes = ( ( title->duration / 90000 ) % 3600 ) / 60;
190 title->seconds = ( title->duration / 90000 ) % 60;
191 hb_log( "scan: duration is %02d:%02d:%02d (%lld ms)",
192 title->hours, title->minutes, title->seconds,
193 title->duration / 90 );
195 /* Discard titles under 10 seconds */
196 if( !( title->hours | title->minutes ) && title->seconds < 10 )
198 hb_log( "scan: ignoring title (too short)" );
202 /* Detect languages */
203 for( i = 0; i < vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
205 hb_audio_t * audio, * audio_tmp;
206 int audio_format, lang_code, audio_control,
209 hb_log( "scan: checking audio %d", i + 1 );
211 audio = calloc( sizeof( hb_audio_t ), 1 );
213 audio_format = vts->vtsi_mat->vts_audio_attr[i].audio_format;
214 lang_code = vts->vtsi_mat->vts_audio_attr[i].lang_code;
216 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i];
218 if( !( audio_control & 0x8000 ) )
220 hb_log( "scan: audio channel is not active" );
225 position = ( audio_control & 0x7F00 ) >> 8;
227 switch( audio_format )
230 audio->id = ( ( 0x80 + position ) << 8 ) | 0xbd;
231 audio->codec = HB_ACODEC_AC3;
236 audio->id = 0xc0 + position;
237 audio->codec = HB_ACODEC_MPGA;
241 audio->id = ( ( 0xa0 + position ) << 8 ) | 0xbd;
242 audio->codec = HB_ACODEC_LPCM;
248 hb_log( "scan: unknown audio codec (%x)",
257 /* Check for duplicate tracks */
259 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
261 audio_tmp = hb_list_item( title->list_audio, j );
262 if( audio->id == audio_tmp->id )
270 hb_log( "scan: duplicate audio track" );
275 snprintf( audio->lang, sizeof( audio->lang ), "%s (%s)",
276 lang_for_code( vts->vtsi_mat->vts_audio_attr[i].lang_code ),
277 audio->codec == HB_ACODEC_AC3 ? "AC3" : ( audio->codec ==
278 HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) );
279 snprintf( audio->lang_simple, sizeof( audio->lang_simple ), "%s",
280 lang_for_code( vts->vtsi_mat->vts_audio_attr[i].lang_code ) );
282 hb_log( "scan: id=%x, lang=%s", audio->id,
285 hb_list_add( title->list_audio, audio );
288 if( !hb_list_count( title->list_audio ) )
290 hb_log( "scan: ignoring title (no audio track)" );
294 memcpy( title->palette,
295 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette,
296 16 * sizeof( uint32_t ) );
298 /* Check for subtitles */
299 for( i = 0; i < vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
301 hb_subtitle_t * subtitle;
305 hb_log( "scan: checking subtitle %d", i + 1 );
308 vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i];
310 if( !( spu_control & 0x80000000 ) )
312 hb_log( "scan: subtitle channel is not active" );
316 if( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
318 switch( vts->vtsi_mat->vts_video_attr.permitted_df )
321 position = spu_control & 0xFF;
324 position = ( spu_control >> 8 ) & 0xFF;
327 position = ( spu_control >> 16 ) & 0xFF;
332 position = ( spu_control >> 24 ) & 0x7F;
335 subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
336 subtitle->id = ( ( 0x20 + position ) << 8 ) | 0xbd;
337 snprintf( subtitle->lang, sizeof( subtitle->lang ), "%s",
338 lang_for_code( vts->vtsi_mat->vts_subp_attr[i].lang_code ) );
340 hb_log( "scan: id=%x, lang=%s", subtitle->id,
343 hb_list_add( title->list_subtitle, subtitle );
347 hb_log( "scan: title %d has %d chapters", t,
348 vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts );
350 i < vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts; i++ )
352 int pgc_id_next, pgn_next;
355 chapter = calloc( sizeof( hb_chapter_t ), 1 );
358 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgcn;
359 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgn;
360 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
363 chapter->cell_start = d->pgc->program_map[pgn-1] - 1;
364 chapter->block_start =
365 d->pgc->cell_playback[chapter->cell_start].first_sector;
368 if( i != vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts - 1 )
370 /* The cell before the starting cell of the next chapter,
372 pgc_id_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgcn;
373 pgn_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgn;
374 pgc_next = vts->vts_pgcit->pgci_srp[pgc_id_next-1].pgc;
375 chapter->cell_end = pgc_next->program_map[pgn_next-1] - 2;
376 if( chapter->cell_end < 0 )
385 /* ... the last cell of the title */
386 chapter->cell_end = title->cell_end;
389 d->pgc->cell_playback[chapter->cell_end].last_sector;
391 /* Block count, duration */
392 pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
393 pgn = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
394 d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
395 chapter->block_count = 0;
396 chapter->duration = 0;
398 d->cell_cur = chapter->cell_start;
399 while( d->cell_cur <= chapter->cell_end )
401 #define cp d->pgc->cell_playback[d->cell_cur]
402 chapter->block_count += cp.last_sector + 1 - cp.first_sector;
403 chapter->duration += 90LL * dvdtime2msec( &cp.playback_time );
406 d->cell_cur = d->cell_next;
409 if( chapter->block_count < 2048 && chapter->index > 1 )
411 hb_log( "scan: chapter %d too short (%d blocks, "
412 "cells=%d->%d), merging", chapter->index,
413 chapter->block_count, chapter->cell_start,
415 chapter_old = hb_list_item( title->list_chapter,
416 chapter->index - 2 );
417 chapter_old->cell_end = chapter->cell_end;
418 chapter_old->block_end = chapter->block_end;
419 chapter_old->block_count += chapter->block_count;
421 chapter = chapter_old;
425 hb_list_add( title->list_chapter, chapter );
430 /* The durations we get for chapters aren't precise. Scale them so
431 the total matches the title duration */
433 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
435 chapter = hb_list_item( title->list_chapter, i );
436 duration += chapter->duration;
438 duration_correction = (float) title->duration / (float) duration;
439 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
442 chapter = hb_list_item( title->list_chapter, i );
443 chapter->duration = duration_correction * chapter->duration;
444 seconds = ( chapter->duration + 45000 ) / 90000;
445 chapter->hours = seconds / 3600;
446 chapter->minutes = ( seconds % 3600 ) / 60;
447 chapter->seconds = seconds % 60;
449 hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %lld ms",
450 chapter->index, chapter->cell_start, chapter->cell_end,
451 chapter->block_start, chapter->block_end,
452 chapter->block_count, chapter->duration / 90 );
455 /* Get aspect. We don't get width/height/rate infos here as
456 they tend to be wrong */
457 switch( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
460 title->aspect = HB_ASPECT_BASE * 4 / 3;
463 title->aspect = HB_ASPECT_BASE * 16 / 9;
466 hb_log( "scan: unknown aspect" );
470 hb_log( "scan: aspect = %d", title->aspect );
472 /* This title is ok so far */
476 hb_list_close( &title->list_audio );
481 if( vts ) ifoClose( vts );
486 /***********************************************************************
488 ***********************************************************************
489 * Title and chapter start at 1
490 **********************************************************************/
491 int hb_dvd_start( hb_dvd_t * d, int title, int chapter )
496 /* Open the IFO and the VOBs for this title */
497 d->vts = d->vmg->tt_srpt->title[title-1].title_set_nr;
498 d->ttn = d->vmg->tt_srpt->title[title-1].vts_ttn;
499 if( !( d->ifo = ifoOpen( d->reader, d->vts ) ) )
501 hb_log( "dvd: ifoOpen failed for VTS %d", d->vts );
504 if( !( d->file = DVDOpenFile( d->reader, d->vts,
505 DVD_READ_TITLE_VOBS ) ) )
507 hb_log( "dvd: DVDOpenFile failed for VTS %d", d->vts );
511 /* Get title first and last blocks */
512 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgcn;
513 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgn;
514 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
515 d->cell_start = d->pgc->program_map[pgn - 1] - 1;
516 d->cell_end = d->pgc->nr_of_cells - 1;
517 d->title_start = d->pgc->cell_playback[d->cell_start].first_sector;
518 d->title_end = d->pgc->cell_playback[d->cell_end].last_sector;
521 d->title_block_count = 0;
522 for( i = d->cell_start; i <= d->cell_end; i++ )
524 d->title_block_count += d->pgc->cell_playback[i].last_sector + 1 -
525 d->pgc->cell_playback[i].first_sector;
528 /* Get pgc for the current chapter */
529 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgcn;
530 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgn;
531 d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
533 /* Get the two first cells */
534 d->cell_cur = d->pgc->program_map[pgn-1] - 1;
537 d->block = d->pgc->cell_playback[d->cell_cur].first_sector;
538 d->next_vobu = d->block;
544 /***********************************************************************
546 ***********************************************************************
548 **********************************************************************/
549 void hb_dvd_stop( hb_dvd_t * d )
558 DVDCloseFile( d->file );
563 /***********************************************************************
565 ***********************************************************************
567 **********************************************************************/
568 int hb_dvd_seek( hb_dvd_t * d, float f )
573 count = f * d->title_block_count;
575 for( i = d->cell_start; i <= d->cell_end; i++ )
577 sizeCell = d->pgc->cell_playback[i].last_sector + 1 -
578 d->pgc->cell_playback[i].first_sector;
580 if( count < sizeCell )
585 /* Now let hb_dvd_read find the next VOBU */
586 d->next_vobu = d->pgc->cell_playback[i].first_sector + count;
594 if( i > d->cell_end )
602 /***********************************************************************
604 ***********************************************************************
606 **********************************************************************/
607 int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
619 int block, pack_len, next_vobu;
621 if( DVDReadBlocks( d->file, d->next_vobu, 1, b->data ) != 1 )
623 hb_log( "dvd: DVDReadBlocks failed (%d)", d->next_vobu );
627 navRead_DSI( &dsi_pack, &b->data[DSI_START_BYTE] );
629 block = dsi_pack.dsi_gi.nv_pck_lbn;
630 pack_len = dsi_pack.dsi_gi.vobu_ea;
631 next_vobu = block + ( dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
635 block >= d->next_vobu &&
636 ( block <= d->title_start + d->title_block_count ||
637 block <= d->title_end ) )
640 This looks like a valid VOBU, but actually we are
645 hb_log( "dvd: found VOBU at %d (b %d, l %d, n %d)",
646 d->next_vobu, block, pack_len, next_vobu );
650 d->pack_len = pack_len;
651 d->next_vobu = next_vobu;
655 /* Wasn't a valid VOBU, try next block */
659 hb_log( "dvd: looking for a VOBU (%d)", d->next_vobu );
665 hb_log( "dvd: couldn't find a VOBU after 1024 blocks" );
672 if( dsi_pack.vobu_sri.next_vobu == SRI_END_OF_CELL )
674 d->cell_cur = d->cell_next;
675 d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
681 if( DVDReadBlocks( d->file, d->block, 1, b->data ) != 1 )
683 hb_log( "reader: DVDReadBlocks failed (%d)", d->block );
694 /***********************************************************************
696 ***********************************************************************
697 * Returns in which chapter the next block to be read is.
698 * Chapter numbers start at 1.
699 **********************************************************************/
700 int hb_dvd_chapter( hb_dvd_t * d )
707 i < d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
710 /* Get pgc for chapter (i+1) */
711 pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
712 pgn = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
713 pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
715 if( d->cell_cur >= pgc->program_map[pgn-1] - 1 &&
716 d->cell_cur <= pgc->nr_of_cells - 1 )
718 /* We are in this chapter */
727 /***********************************************************************
729 ***********************************************************************
730 * Closes and frees everything
731 **********************************************************************/
732 void hb_dvd_close( hb_dvd_t ** _d )
742 DVDClose( d->reader );
749 /***********************************************************************
751 ***********************************************************************
752 * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
753 * cell to be read when we will be done with cell_cur.
754 **********************************************************************/
755 static void FindNextCell( hb_dvd_t * d )
759 if( d->pgc->cell_playback[d->cell_cur].block_type ==
760 BLOCK_TYPE_ANGLE_BLOCK )
763 while( d->pgc->cell_playback[d->cell_cur+i].block_mode !=
764 BLOCK_MODE_LAST_CELL )
768 d->cell_next = d->cell_cur + i + 1;
772 d->cell_next = d->cell_cur + 1;
776 /***********************************************************************
778 ***********************************************************************
780 **********************************************************************/
781 static int dvdtime2msec(dvd_time_t * dt)
783 double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
784 double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
786 ms = (((dt->hour & 0xf0) >> 3) * 5 + (dt->hour & 0x0f)) * 3600000;
787 ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
788 ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
792 ms += ((dt->frame_u & 0x30) >> 3) * 5 +
793 (dt->frame_u & 0x0f) * 1000.0 / fps;