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.fr/>.
5 It may be used under the terms of the GNU General Public License. */
11 #include "libbluray/bluray.h"
24 /***********************************************************************
26 **********************************************************************/
27 static int next_packet( BLURAY *bd, uint8_t *pkt );
29 /***********************************************************************
31 ***********************************************************************
33 **********************************************************************/
34 hb_bd_t * hb_bd_init( char * path )
38 d = calloc( sizeof( hb_bd_t ), 1 );
41 d->bd = bd_open( path, NULL );
45 * Not an error, may be a stream - which we'll try in a moment.
47 hb_log( "bd: not a bd - trying as a stream/file instead" );
51 d->title_count = bd_get_titles( d->bd, TITLES_RELEVANT );
52 if ( d->title_count == 0 )
54 hb_log( "bd: not a bd - trying as a stream/file instead" );
57 d->path = strdup( path );
62 if( d->bd ) bd_close( d->bd );
67 /***********************************************************************
69 **********************************************************************/
70 int hb_bd_title_count( hb_bd_t * d )
72 return d->title_count;
75 /***********************************************************************
77 **********************************************************************/
78 hb_title_t * hb_bd_title_scan( hb_bd_t * d, int tt, uint64_t min_duration )
82 hb_chapter_t * chapter;
84 BLURAY_TITLE_INFO * ti = NULL;
86 hb_log( "bd: scanning title %d", tt );
88 title = hb_title_init( d->path, tt );
89 title->demuxer = HB_MPEG2_TS_DEMUXER;
90 title->type = HB_BD_TYPE;
91 title->reg_desc = STR4_TO_UINT32("HDMV");
93 char * p_cur, * p_last = d->path;
94 for( p_cur = d->path; *p_cur; p_cur++ )
96 if( p_cur[0] == '/' && p_cur[1] )
101 snprintf( title->name, sizeof( title->name ), "%s", p_last );
102 strncpy( title->path, d->path, 1024 );
103 title->path[1023] = 0;
108 ti = bd_get_title_info( d->bd, tt - 1 );
111 hb_log( "bd: invalid title" );
114 if ( ti->clip_count == 0 )
116 hb_log( "bd: stream has no clips" );
119 if ( ti->clips[0].video_stream_count == 0 )
121 hb_log( "bd: stream has no video" );
125 uint64_t pkt_count = 0;
126 for ( ii = 0; ii < ti->clip_count; ii++ )
128 pkt_count += ti->clips[ii].pkt_count;
130 title->block_start = 0;
131 title->block_end = pkt_count;
132 title->block_count = pkt_count;
134 title->angle_count = ti->angle_count;
137 title->duration = ti->duration;
138 title->hours = title->duration / 90000 / 3600;
139 title->minutes = ( ( title->duration / 90000 ) % 3600 ) / 60;
140 title->seconds = ( title->duration / 90000 ) % 60;
141 hb_log( "bd: duration is %02d:%02d:%02d (%"PRId64" ms)",
142 title->hours, title->minutes, title->seconds,
143 title->duration / 90 );
145 /* ignore short titles because they're often stills */
146 if( ti->duration < min_duration )
148 hb_log( "bd: ignoring title (too short)" );
152 BLURAY_STREAM_INFO * bdvideo = &ti->clips[0].video_streams[0];
154 title->video_id = bdvideo->pid;
155 title->video_stream_type = bdvideo->coding_type;
157 hb_log( "bd: video id=%x, stream type=%s, format %s", title->video_id,
158 bdvideo->coding_type == BLURAY_STREAM_TYPE_VIDEO_MPEG1 ? "MPEG1" :
159 bdvideo->coding_type == BLURAY_STREAM_TYPE_VIDEO_MPEG2 ? "MPEG2" :
160 bdvideo->coding_type == BLURAY_STREAM_TYPE_VIDEO_VC1 ? "VC-1" :
161 bdvideo->coding_type == BLURAY_STREAM_TYPE_VIDEO_H264 ? "H264" :
163 bdvideo->format == BLURAY_VIDEO_FORMAT_480I ? "480i" :
164 bdvideo->format == BLURAY_VIDEO_FORMAT_576I ? "576i" :
165 bdvideo->format == BLURAY_VIDEO_FORMAT_480P ? "480p" :
166 bdvideo->format == BLURAY_VIDEO_FORMAT_1080I ? "1080i" :
167 bdvideo->format == BLURAY_VIDEO_FORMAT_720P ? "720p" :
168 bdvideo->format == BLURAY_VIDEO_FORMAT_1080P ? "1080p" :
169 bdvideo->format == BLURAY_VIDEO_FORMAT_576P ? "576p" :
173 if ( bdvideo->coding_type == BLURAY_STREAM_TYPE_VIDEO_VC1 &&
174 ( bdvideo->format == BLURAY_VIDEO_FORMAT_480I ||
175 bdvideo->format == BLURAY_VIDEO_FORMAT_576I ||
176 bdvideo->format == BLURAY_VIDEO_FORMAT_1080I ) )
178 hb_log( "bd: Interlaced VC-1 not supported" );
182 switch( bdvideo->coding_type )
184 case BLURAY_STREAM_TYPE_VIDEO_MPEG1:
185 case BLURAY_STREAM_TYPE_VIDEO_MPEG2:
186 title->video_codec = WORK_DECMPEG2;
187 title->video_codec_param = 0;
190 case BLURAY_STREAM_TYPE_VIDEO_VC1:
191 title->video_codec = WORK_DECAVCODECV;
192 title->video_codec_param = CODEC_ID_VC1;
195 case BLURAY_STREAM_TYPE_VIDEO_H264:
196 title->video_codec = WORK_DECAVCODECV;
197 title->video_codec_param = CODEC_ID_H264;
198 title->flags |= HBTF_NO_IDR;
202 hb_log( "scan: unknown video codec (%x)",
203 bdvideo->coding_type );
207 switch ( bdvideo->aspect )
209 case BLURAY_ASPECT_RATIO_4_3:
210 title->container_aspect = 4. / 3.;
212 case BLURAY_ASPECT_RATIO_16_9:
213 title->container_aspect = 16. / 9.;
216 hb_log( "bd: unknown aspect" );
219 hb_log( "bd: aspect = %g", title->container_aspect );
222 // The BD may have clips that have no audio tracks, so scan
223 // the list of clips for one that has audio.
225 int audio_clip_index = 0;
226 for ( ii = 0; ii < ti->clip_count; ii++ )
228 if ( most_audio < ti->clips[ii].audio_stream_count )
230 most_audio = ti->clips[ii].audio_stream_count;
231 audio_clip_index = ii;
234 // Add all the audios found in the above clip.
235 for ( ii = 0; ii < ti->clips[audio_clip_index].audio_stream_count; ii++ )
238 iso639_lang_t * lang;
239 BLURAY_STREAM_INFO * bdaudio;
241 bdaudio = &ti->clips[audio_clip_index].audio_streams[ii];
243 hb_log( "bd: checking audio %d", ii + 1 );
245 audio = calloc( sizeof( hb_audio_t ), 1 );
247 audio->id = bdaudio->pid;
249 audio->config.in.stream_type = bdaudio->coding_type;
250 switch( bdaudio->coding_type )
252 case BLURAY_STREAM_TYPE_AUDIO_AC3:
253 case BLURAY_STREAM_TYPE_AUDIO_TRUHD:
254 audio->config.in.codec = HB_ACODEC_AC3;
255 audio->config.in.codec_param = 0;
258 case BLURAY_STREAM_TYPE_AUDIO_LPCM:
259 audio->config.in.codec = HB_ACODEC_MPGA;
260 audio->config.in.codec_param = CODEC_ID_PCM_BLURAY;
263 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS:
264 audio->config.in.codec = HB_ACODEC_MPGA;
265 audio->config.in.codec_param = CODEC_ID_EAC3;
268 case BLURAY_STREAM_TYPE_AUDIO_MPEG1:
269 case BLURAY_STREAM_TYPE_AUDIO_MPEG2:
270 audio->config.in.codec = HB_ACODEC_MPGA;
271 audio->config.in.codec_param = CODEC_ID_MP2;
274 case BLURAY_STREAM_TYPE_AUDIO_DTS:
275 case BLURAY_STREAM_TYPE_AUDIO_DTSHD:
276 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_MASTER:
277 audio->config.in.codec = HB_ACODEC_DCA;
278 audio->config.in.codec_param = 0;
282 audio->config.in.codec = 0;
283 hb_log( "scan: unknown audio codec (%x)",
284 bdaudio->coding_type );
288 audio->config.lang.type = 0;
289 lang = lang_for_code2( (char*)bdaudio->lang );
291 snprintf( audio->config.lang.description,
292 sizeof( audio->config.lang.description ), "%s (%s)",
293 strlen(lang->native_name) ? lang->native_name :
295 audio->config.in.codec == HB_ACODEC_AC3 ? "AC3" :
296 ( audio->config.in.codec == HB_ACODEC_DCA ? "DTS" :
297 ( audio->config.in.codec == HB_ACODEC_MPGA ?
298 ( audio->config.in.codec_param == CODEC_ID_PCM_BLURAY ? "LPCM" :
299 ( audio->config.in.codec_param == CODEC_ID_EAC3 ? "E-AC3" :
300 ( audio->config.in.codec_param == CODEC_ID_MP2 ? "MPEG" :
305 snprintf( audio->config.lang.simple,
306 sizeof( audio->config.lang.simple ), "%s",
307 strlen(lang->native_name) ? lang->native_name :
310 snprintf( audio->config.lang.iso639_2,
311 sizeof( audio->config.lang.iso639_2 ), "%s", lang->iso639_2);
313 hb_log( "bd: audio id=%x, lang=%s, 3cc=%s", audio->id,
314 audio->config.lang.description, audio->config.lang.iso639_2 );
316 audio->config.in.track = ii;
317 hb_list_add( title->list_audio, audio );
321 for ( ii = 0; ii < ti->chapter_count; ii++ )
323 chapter = calloc( sizeof( hb_chapter_t ), 1 );
325 chapter->index = ii + 1;
326 chapter->duration = ti->chapters[ii].duration;
327 chapter->block_start = ti->chapters[ii].offset;
330 seconds = ( chapter->duration + 45000 ) / 90000;
331 chapter->hours = seconds / 3600;
332 chapter->minutes = ( seconds % 3600 ) / 60;
333 chapter->seconds = seconds % 60;
335 hb_log( "bd: chap %d packet=%"PRIu64", %"PRId64" ms",
337 chapter->block_start,
338 chapter->duration / 90 );
340 hb_list_add( title->list_chapter, chapter );
342 hb_log( "bd: title %d has %d chapters", tt, ti->chapter_count );
344 /* This title is ok so far */
348 hb_list_close( &title->list_audio );
353 if ( ti ) bd_free_title_info( ti );
358 /***********************************************************************
360 **********************************************************************/
361 int hb_bd_main_feature( hb_bd_t * d, hb_list_t * list_title )
365 uint64_t longest_duration = 0;
366 int highest_rank = 0;
367 int rank[8] = {0, 1, 3, 2, 6, 5, 7, 4};
368 BLURAY_TITLE_INFO * ti;
370 for ( ii = 0; ii < hb_list_count( list_title ); ii++ )
372 hb_title_t * title = hb_list_item( list_title, ii );
373 ti = bd_get_title_info( d->bd, title->index - 1 );
376 BLURAY_STREAM_INFO * bdvideo = &ti->clips[0].video_streams[0];
377 if ( title->duration > longest_duration * 0.7 && bdvideo->format < 8 )
379 if (highest_rank < rank[bdvideo->format] ||
380 ( title->duration > longest_duration &&
381 highest_rank == rank[bdvideo->format]))
383 longest = title->index;
384 longest_duration = title->duration;
385 highest_rank = rank[bdvideo->format];
388 bd_free_title_info( ti );
390 else if ( title->duration > longest_duration )
392 longest_duration = title->duration;
393 longest = title->index;
399 /***********************************************************************
401 ***********************************************************************
402 * Title and chapter start at 1
403 **********************************************************************/
404 int hb_bd_start( hb_bd_t * d, hb_title_t *title )
408 d->pkt_count = title->block_count;
410 // Calling bd_get_event initializes libbluray event queue.
411 bd_select_title( d->bd, title->index - 1 );
412 bd_get_event( d->bd, &event );
414 d->stream = hb_bd_stream_open( title );
415 if ( d->stream == NULL )
422 /***********************************************************************
424 ***********************************************************************
426 **********************************************************************/
427 void hb_bd_stop( hb_bd_t * d )
429 if( d->stream ) hb_stream_close( &d->stream );
432 /***********************************************************************
434 ***********************************************************************
436 **********************************************************************/
437 int hb_bd_seek( hb_bd_t * d, float f )
439 uint64_t packet = f * d->pkt_count;
441 bd_seek(d->bd, packet * 192);
442 d->next_chap = bd_get_current_chapter( d->bd ) + 1;
446 int hb_bd_seek_pts( hb_bd_t * d, uint64_t pts )
448 bd_seek_time(d->bd, pts);
449 d->next_chap = bd_get_current_chapter( d->bd ) + 1;
453 int hb_bd_seek_chapter( hb_bd_t * d, int c )
457 pos = bd_seek_chapter( d->bd, c - 1 );
461 /***********************************************************************
463 ***********************************************************************
465 **********************************************************************/
466 int hb_bd_read( hb_bd_t * d, hb_buffer_t * b )
476 if ( d->next_chap != d->chapter )
478 b->new_chap = d->chapter = d->next_chap;
480 result = next_packet( d->bd, buf );
483 hb_error("bd: Read Error");
484 pos = bd_tell( d->bd );
485 bd_seek( d->bd, pos + 192 );
487 if (error_count > 10)
489 hb_error("bd: Error, too many consecutive read errors");
494 else if ( result == 0 )
500 while ( bd_get_event( d->bd, &event ) )
502 switch ( event.event )
504 case BD_EVENT_CHAPTER:
505 // The muxers expect to only get chapter 2 and above
506 // They write chapter 1 when chapter 2 is detected.
507 d->next_chap = event.param;
514 // buf+4 to skip the BD timestamp at start of packet
515 result = hb_ts_decode_pkt( d->stream, buf+4, b );
524 /***********************************************************************
526 ***********************************************************************
527 * Returns in which chapter the next block to be read is.
528 * Chapter numbers start at 1.
529 **********************************************************************/
530 int hb_bd_chapter( hb_bd_t * d )
535 /***********************************************************************
537 ***********************************************************************
538 * Closes and frees everything
539 **********************************************************************/
540 void hb_bd_close( hb_bd_t ** _d )
544 if( d->stream ) hb_stream_close( &d->stream );
545 if( d->bd ) bd_close( d->bd );
551 /***********************************************************************
553 ***********************************************************************
554 * Sets the angle to read
555 **********************************************************************/
556 void hb_bd_set_angle( hb_bd_t * d, int angle )
559 if ( !bd_select_angle( d->bd, angle) )
561 hb_log("bd_select_angle failed");
565 static int check_ts_sync(const uint8_t *buf)
567 // must have initial sync byte, no scrambling & a legal adaptation ctrl
568 return (buf[0] == 0x47) && ((buf[3] >> 6) == 0) && ((buf[3] >> 4) > 0);
571 static int have_ts_sync(const uint8_t *buf, int psize)
573 return check_ts_sync(&buf[0*psize]) && check_ts_sync(&buf[1*psize]) &&
574 check_ts_sync(&buf[2*psize]) && check_ts_sync(&buf[3*psize]) &&
575 check_ts_sync(&buf[4*psize]) && check_ts_sync(&buf[5*psize]) &&
576 check_ts_sync(&buf[6*psize]) && check_ts_sync(&buf[7*psize]);
579 #define MAX_HOLE 192*80
581 static uint64_t align_to_next_packet(BLURAY *bd, uint8_t *pkt)
583 uint8_t buf[MAX_HOLE];
585 uint64_t start = bd_tell(bd);
589 memcpy(buf, pkt, 192);
590 if ( start >= 192 ) {
597 if (bd_read(bd, buf+off, sizeof(buf)-off) == sizeof(buf)-off)
599 const uint8_t *bp = buf;
602 for ( i = sizeof(buf) - 8 * 192; --i >= 0; ++bp )
604 if ( have_ts_sync( bp, 192 ) )
615 memcpy(buf, buf + sizeof(buf) - off, off);
616 start += sizeof(buf) - off;
623 off = start + pos - 4;
624 // bd_seek seeks to the nearest access unit *before* the requested position
625 // we don't want to seek backwards, so we need to read until we get
626 // past that position.
628 while (off > bd_tell(bd))
630 if (bd_read(bd, buf, 192) != 192)
635 return start - orig + pos;
638 static int next_packet( BLURAY *bd, uint8_t *pkt )
644 result = bd_read( bd, pkt, 192 );
653 // Sync byte is byte 4. 0-3 are timestamp.
658 // lost sync - back up to where we started then try to re-establish.
659 uint64_t pos = bd_tell(bd);
660 uint64_t pos2 = align_to_next_packet(bd, pkt);
663 hb_log( "next_packet: eof while re-establishing sync @ %"PRId64, pos );
666 hb_log( "next_packet: sync lost @ %"PRId64", regained after %"PRId64" bytes",