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 ? "MPEG" :
300 snprintf( audio->config.lang.simple,
301 sizeof( audio->config.lang.simple ), "%s",
302 strlen(lang->native_name) ? lang->native_name :
305 snprintf( audio->config.lang.iso639_2,
306 sizeof( audio->config.lang.iso639_2 ), "%s", lang->iso639_2);
308 hb_log( "bd: audio id=%x, lang=%s, 3cc=%s", audio->id,
309 audio->config.lang.description, audio->config.lang.iso639_2 );
311 audio->config.in.track = ii;
312 hb_list_add( title->list_audio, audio );
316 for ( ii = 0; ii < ti->chapter_count; ii++ )
318 chapter = calloc( sizeof( hb_chapter_t ), 1 );
320 chapter->index = ii + 1;
321 chapter->duration = ti->chapters[ii].duration;
322 chapter->block_start = ti->chapters[ii].offset;
325 seconds = ( chapter->duration + 45000 ) / 90000;
326 chapter->hours = seconds / 3600;
327 chapter->minutes = ( seconds % 3600 ) / 60;
328 chapter->seconds = seconds % 60;
330 hb_log( "bd: chap %d packet=%"PRIu64", %"PRId64" ms",
332 chapter->block_start,
333 chapter->duration / 90 );
335 hb_list_add( title->list_chapter, chapter );
337 hb_log( "bd: title %d has %d chapters", tt, ti->chapter_count );
339 /* This title is ok so far */
343 hb_list_close( &title->list_audio );
348 if ( ti ) bd_free_title_info( ti );
353 /***********************************************************************
355 **********************************************************************/
356 int hb_bd_main_feature( hb_bd_t * d, hb_list_t * list_title )
360 uint64_t longest_duration = 0;
361 int highest_rank = 0;
362 int rank[8] = {0, 1, 3, 2, 6, 5, 7, 4};
363 BLURAY_TITLE_INFO * ti;
365 for ( ii = 0; ii < hb_list_count( list_title ); ii++ )
367 hb_title_t * title = hb_list_item( list_title, ii );
368 ti = bd_get_title_info( d->bd, title->index - 1 );
371 BLURAY_STREAM_INFO * bdvideo = &ti->clips[0].video_streams[0];
372 if ( title->duration > longest_duration * 0.7 && bdvideo->format < 8 )
374 if (highest_rank < rank[bdvideo->format] ||
375 ( title->duration > longest_duration &&
376 highest_rank == rank[bdvideo->format]))
378 longest = title->index;
379 longest_duration = title->duration;
380 highest_rank = rank[bdvideo->format];
383 bd_free_title_info( ti );
385 else if ( title->duration > longest_duration )
387 longest_duration = title->duration;
388 longest = title->index;
394 /***********************************************************************
396 ***********************************************************************
397 * Title and chapter start at 1
398 **********************************************************************/
399 int hb_bd_start( hb_bd_t * d, hb_title_t *title )
403 d->pkt_count = title->block_count;
405 // Calling bd_get_event initializes libbluray event queue.
406 bd_select_title( d->bd, title->index - 1 );
407 bd_get_event( d->bd, &event );
409 d->stream = hb_bd_stream_open( title );
410 if ( d->stream == NULL )
417 /***********************************************************************
419 ***********************************************************************
421 **********************************************************************/
422 void hb_bd_stop( hb_bd_t * d )
424 if( d->stream ) hb_stream_close( &d->stream );
427 /***********************************************************************
429 ***********************************************************************
431 **********************************************************************/
432 int hb_bd_seek( hb_bd_t * d, float f )
434 uint64_t packet = f * d->pkt_count;
436 bd_seek(d->bd, packet * 192);
437 d->next_chap = bd_get_current_chapter( d->bd ) + 1;
441 int hb_bd_seek_pts( hb_bd_t * d, uint64_t pts )
443 bd_seek_time(d->bd, pts);
444 d->next_chap = bd_get_current_chapter( d->bd ) + 1;
448 int hb_bd_seek_chapter( hb_bd_t * d, int c )
452 pos = bd_seek_chapter( d->bd, c - 1 );
456 /***********************************************************************
458 ***********************************************************************
460 **********************************************************************/
461 int hb_bd_read( hb_bd_t * d, hb_buffer_t * b )
471 if ( d->next_chap != d->chapter )
473 b->new_chap = d->chapter = d->next_chap;
475 result = next_packet( d->bd, buf );
478 hb_error("bd: Read Error");
479 pos = bd_tell( d->bd );
480 bd_seek( d->bd, pos + 192 );
482 if (error_count > 10)
484 hb_error("bd: Error, too many consecutive read errors");
489 else if ( result == 0 )
495 while ( bd_get_event( d->bd, &event ) )
497 switch ( event.event )
499 case BD_EVENT_CHAPTER:
500 // The muxers expect to only get chapter 2 and above
501 // They write chapter 1 when chapter 2 is detected.
502 d->next_chap = event.param;
509 // buf+4 to skip the BD timestamp at start of packet
510 result = hb_ts_decode_pkt( d->stream, buf+4, b );
519 /***********************************************************************
521 ***********************************************************************
522 * Returns in which chapter the next block to be read is.
523 * Chapter numbers start at 1.
524 **********************************************************************/
525 int hb_bd_chapter( hb_bd_t * d )
530 /***********************************************************************
532 ***********************************************************************
533 * Closes and frees everything
534 **********************************************************************/
535 void hb_bd_close( hb_bd_t ** _d )
539 if( d->stream ) hb_stream_close( &d->stream );
540 if( d->bd ) bd_close( d->bd );
546 /***********************************************************************
548 ***********************************************************************
549 * Sets the angle to read
550 **********************************************************************/
551 void hb_bd_set_angle( hb_bd_t * d, int angle )
554 if ( !bd_select_angle( d->bd, angle) )
556 hb_log("bd_select_angle failed");
560 static int check_ts_sync(const uint8_t *buf)
562 // must have initial sync byte, no scrambling & a legal adaptation ctrl
563 return (buf[0] == 0x47) && ((buf[3] >> 6) == 0) && ((buf[3] >> 4) > 0);
566 static int have_ts_sync(const uint8_t *buf, int psize)
568 return check_ts_sync(&buf[0*psize]) && check_ts_sync(&buf[1*psize]) &&
569 check_ts_sync(&buf[2*psize]) && check_ts_sync(&buf[3*psize]) &&
570 check_ts_sync(&buf[4*psize]) && check_ts_sync(&buf[5*psize]) &&
571 check_ts_sync(&buf[6*psize]) && check_ts_sync(&buf[7*psize]);
574 #define MAX_HOLE 192*80
576 static uint64_t align_to_next_packet(BLURAY *bd, uint8_t *pkt)
578 uint8_t buf[MAX_HOLE];
580 uint64_t start = bd_tell(bd);
584 memcpy(buf, pkt, 192);
585 if ( start >= 192 ) {
592 if (bd_read(bd, buf+off, sizeof(buf)-off) == sizeof(buf)-off)
594 const uint8_t *bp = buf;
597 for ( i = sizeof(buf) - 8 * 192; --i >= 0; ++bp )
599 if ( have_ts_sync( bp, 192 ) )
610 memcpy(buf, buf + sizeof(buf) - off, off);
611 start += sizeof(buf) - off;
618 off = start + pos - 4;
619 // bd_seek seeks to the nearest access unit *before* the requested position
620 // we don't want to seek backwards, so we need to read until we get
621 // past that position.
623 while (off > bd_tell(bd))
625 if (bd_read(bd, buf, 192) != 192)
630 return start - orig + pos;
633 static int next_packet( BLURAY *bd, uint8_t *pkt )
639 result = bd_read( bd, pkt, 192 );
648 // Sync byte is byte 4. 0-3 are timestamp.
653 // lost sync - back up to where we started then try to re-establish.
654 uint64_t pos = bd_tell(bd);
655 uint64_t pos2 = align_to_next_packet(bd, pkt);
658 hb_log( "next_packet: eof while re-establishing sync @ %"PRId64, pos );
661 hb_log( "next_packet: sync lost @ %"PRId64", regained after %"PRId64" bytes",