OSDN Git Service

ca09ce733ad0d99f79c859795acef07f1d6c4057
[handbrake-jp/handbrake-jp-git.git] / libhb / bd.c
1 /* $Id: dvd.c,v 1.12 2005/11/25 15:05:25 titer Exp $
2
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. */
6
7 #include "hb.h"
8 #include "lang.h"
9 #include "hbffmpeg.h"
10
11 #include "libbluray/bluray.h"
12
13 struct hb_bd_s
14 {
15     char         * path;
16     BLURAY       * bd;
17     int            title_count;
18     uint64_t       pkt_count;
19     hb_stream_t  * stream;
20     int            chapter;
21     int            next_chap;
22 };
23
24 /***********************************************************************
25  * Local prototypes
26  **********************************************************************/
27 static int           next_packet( BLURAY *bd, uint8_t *pkt );
28
29 /***********************************************************************
30  * hb_bd_init
31  ***********************************************************************
32  *
33  **********************************************************************/
34 hb_bd_t * hb_bd_init( char * path )
35 {
36     hb_bd_t * d;
37
38     d = calloc( sizeof( hb_bd_t ), 1 );
39
40     /* Open device */
41     d->bd = bd_open( path, NULL );
42     if( d->bd == NULL )
43     {
44         /*
45          * Not an error, may be a stream - which we'll try in a moment.
46          */
47         hb_log( "bd: not a bd - trying as a stream/file instead" );
48         goto fail;
49     }
50
51     d->title_count = bd_get_titles( d->bd, TITLES_RELEVANT );
52     if ( d->title_count == 0 )
53     {
54         hb_log( "bd: not a bd - trying as a stream/file instead" );
55         goto fail;
56     }
57     d->path = strdup( path );
58
59     return d;
60
61 fail:
62     if( d->bd ) bd_close( d->bd );
63     free( d );
64     return NULL;
65 }
66
67 /***********************************************************************
68  * hb_bd_title_count
69  **********************************************************************/
70 int hb_bd_title_count( hb_bd_t * d )
71 {
72     return d->title_count;
73 }
74
75 /***********************************************************************
76  * hb_bd_title_scan
77  **********************************************************************/
78 hb_title_t * hb_bd_title_scan( hb_bd_t * d, int tt, uint64_t min_duration )
79 {
80
81     hb_title_t   * title;
82     hb_chapter_t * chapter;
83     int            ii;
84     BLURAY_TITLE_INFO * ti = NULL;
85
86     hb_log( "bd: scanning title %d", tt );
87
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");
92
93     char * p_cur, * p_last = d->path;
94     for( p_cur = d->path; *p_cur; p_cur++ )
95     {
96         if( p_cur[0] == '/' && p_cur[1] )
97         {
98             p_last = &p_cur[1];
99         }
100     }
101     snprintf( title->name, sizeof( title->name ), "%s", p_last );
102     strncpy( title->path, d->path, 1024 );
103     title->path[1023] = 0;
104
105     title->vts = 0;
106     title->ttn = 0;
107
108     ti = bd_get_title_info( d->bd, tt - 1 );
109     if ( ti == NULL )
110     {
111         hb_log( "bd: invalid title" );
112         goto fail;
113     }
114     if ( ti->clip_count == 0 )
115     {
116         hb_log( "bd: stream has no clips" );
117         goto fail;
118     }
119     if ( ti->clips[0].video_stream_count == 0 )
120     {
121         hb_log( "bd: stream has no video" );
122         goto fail;
123     }
124
125     uint64_t pkt_count = 0;
126     for ( ii = 0; ii < ti->clip_count; ii++ )
127     {
128         pkt_count += ti->clips[ii].pkt_count;
129     }
130     title->block_start = 0;
131     title->block_end = pkt_count;
132     title->block_count = pkt_count;
133
134     title->angle_count = ti->angle_count;
135
136     /* ignore short titles because they're often stills */
137     if( ti->duration < min_duration )
138     {
139         hb_log( "bd: ignoring title (too short)" );
140         goto fail;
141     }
142
143     /* Get duration */
144     title->duration = ti->duration;
145     title->hours    = title->duration / 90000 / 3600;
146     title->minutes  = ( ( title->duration / 90000 ) % 3600 ) / 60;
147     title->seconds  = ( title->duration / 90000 ) % 60;
148     hb_log( "bd: duration is %02d:%02d:%02d (%"PRId64" ms)",
149             title->hours, title->minutes, title->seconds,
150             title->duration / 90 );
151
152     BLURAY_STREAM_INFO * bdvideo = &ti->clips[0].video_streams[0];
153
154     title->video_id = bdvideo->pid;
155     title->video_stream_type = bdvideo->coding_type;
156
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" :
162             "Unknown",
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" :
170             "Unknown"
171           );
172
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 ) )
177     {
178         hb_log( "bd: Interlaced VC-1 not supported" );
179         goto fail;
180     }
181
182     switch( bdvideo->coding_type )
183     {
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;
188             break;
189
190         case BLURAY_STREAM_TYPE_VIDEO_VC1:
191             title->video_codec = WORK_DECAVCODECV;
192             title->video_codec_param = CODEC_ID_VC1;
193             break;
194
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;
199             break;
200
201         default:
202             hb_log( "scan: unknown video codec (%x)",
203                     bdvideo->coding_type );
204             goto fail;
205     }
206
207     switch ( bdvideo->aspect )
208     {
209         case BLURAY_ASPECT_RATIO_4_3:
210             title->container_aspect = 4. / 3.;
211             break;
212         case BLURAY_ASPECT_RATIO_16_9:
213             title->container_aspect = 16. / 9.;
214             break;
215         default:
216             hb_log( "bd: unknown aspect" );
217             goto fail;
218     }
219     hb_log( "bd: aspect = %g", title->container_aspect );
220
221     /* Detect audio */
222     // The BD may have clips that have no audio tracks, so scan
223     // the list of clips for one that has audio.
224     int most_audio = 0;
225     int audio_clip_index = 0;
226     for ( ii = 0; ii < ti->clip_count; ii++ )
227     {
228         if ( most_audio < ti->clips[ii].audio_stream_count )
229         {
230             most_audio = ti->clips[ii].audio_stream_count;
231             audio_clip_index = ii;
232         }
233     }
234     // Add all the audios found in the above clip.
235     for ( ii = 0; ii < ti->clips[audio_clip_index].audio_stream_count; ii++ )
236     {
237         hb_audio_t * audio;
238         iso639_lang_t * lang;
239         BLURAY_STREAM_INFO * bdaudio;
240
241         bdaudio = &ti->clips[audio_clip_index].audio_streams[ii];
242
243         hb_log( "bd: checking audio %d", ii + 1 );
244
245         audio = calloc( sizeof( hb_audio_t ), 1 );
246
247         audio->id = bdaudio->pid;
248
249         audio->config.in.stream_type = bdaudio->coding_type;
250         switch( bdaudio->coding_type )
251         {
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;
256                 break;
257
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;
261                 break;
262
263             case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS:
264                 audio->config.in.codec = HB_ACODEC_MPGA;
265                 audio->config.in.codec_param = CODEC_ID_AC3;
266                 break;
267
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;
272                 break;
273
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;
279                 break;
280
281             default:
282                 audio->config.in.codec = 0;
283                 hb_log( "scan: unknown audio codec (%x)",
284                         bdaudio->coding_type );
285                 break;
286         }
287
288         audio->config.lang.type = 0;
289         lang = lang_for_code2( (char*)bdaudio->lang );
290
291         snprintf( audio->config.lang.description, 
292                   sizeof( audio->config.lang.description ), "%s (%s)",
293                   strlen(lang->native_name) ? lang->native_name : 
294                                               lang->eng_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" : 
298                                                                "LPCM" ) ) );
299
300         snprintf( audio->config.lang.simple, 
301                   sizeof( audio->config.lang.simple ), "%s",
302                   strlen(lang->native_name) ? lang->native_name : 
303                                               lang->eng_name );
304
305         snprintf( audio->config.lang.iso639_2, 
306                   sizeof( audio->config.lang.iso639_2 ), "%s", lang->iso639_2);
307
308         hb_log( "bd: audio id=%x, lang=%s, 3cc=%s", audio->id,
309                 audio->config.lang.description, audio->config.lang.iso639_2 );
310
311         audio->config.in.track = ii;
312         hb_list_add( title->list_audio, audio );
313     }
314
315     /* Chapters */
316     for ( ii = 0; ii < ti->chapter_count; ii++ )
317     {
318         chapter = calloc( sizeof( hb_chapter_t ), 1 );
319
320         chapter->index = ii + 1;
321         chapter->duration = ti->chapters[ii].duration;
322         chapter->block_start = ti->chapters[ii].offset;
323
324         int seconds;
325         seconds            = ( chapter->duration + 45000 ) / 90000;
326         chapter->hours     = seconds / 3600;
327         chapter->minutes   = ( seconds % 3600 ) / 60;
328         chapter->seconds   = seconds % 60;
329
330         hb_log( "bd: chap %d packet=%"PRIu64", %"PRId64" ms",
331                 chapter->index,
332                 chapter->block_start,
333                 chapter->duration / 90 );
334
335         hb_list_add( title->list_chapter, chapter );
336     }
337     hb_log( "bd: title %d has %d chapters", tt, ti->chapter_count );
338
339     /* This title is ok so far */
340     goto cleanup;
341
342 fail:
343     hb_list_close( &title->list_audio );
344     free( title );
345     title = NULL;
346
347 cleanup:
348     if ( ti ) bd_free_title_info( ti );
349
350     return title;
351 }
352
353 /***********************************************************************
354  * hb_bd_main_feature
355  **********************************************************************/
356 int hb_bd_main_feature( hb_bd_t * d, hb_list_t * list_title )
357 {
358     int longest = 0;
359     int ii;
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;
364
365     for ( ii = 0; ii < hb_list_count( list_title ); ii++ )
366     {
367         hb_title_t * title = hb_list_item( list_title, ii );
368         ti = bd_get_title_info( d->bd, title->index - 1 );
369         if ( ti ) 
370         {
371             BLURAY_STREAM_INFO * bdvideo = &ti->clips[0].video_streams[0];
372             if ( title->duration > longest_duration * 0.7 && bdvideo->format < 8 )
373             {
374                 if (highest_rank < rank[bdvideo->format] ||
375                     ( title->duration > longest_duration &&
376                           highest_rank == rank[bdvideo->format]))
377                 {
378                     longest = title->index;
379                     longest_duration = title->duration;
380                     highest_rank = rank[bdvideo->format];
381                 }
382             }
383             bd_free_title_info( ti );
384         }
385         else if ( title->duration > longest_duration )
386         {
387             longest_duration = title->duration;
388             longest = title->index;
389         }
390     }
391     return longest;
392 }
393
394 /***********************************************************************
395  * hb_bd_start
396  ***********************************************************************
397  * Title and chapter start at 1
398  **********************************************************************/
399 int hb_bd_start( hb_bd_t * d, hb_title_t *title )
400 {
401     BD_EVENT event;
402
403     d->pkt_count = title->block_count;
404
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 );
408     d->chapter = 1;
409     d->stream = hb_bd_stream_open( title );
410     if ( d->stream == NULL )
411     {
412         return 0;
413     }
414     return 1;
415 }
416
417 /***********************************************************************
418  * hb_bd_stop
419  ***********************************************************************
420  *
421  **********************************************************************/
422 void hb_bd_stop( hb_bd_t * d )
423 {
424     if( d->stream ) hb_stream_close( &d->stream );
425 }
426
427 /***********************************************************************
428  * hb_bd_seek
429  ***********************************************************************
430  *
431  **********************************************************************/
432 int hb_bd_seek( hb_bd_t * d, float f )
433 {
434     uint64_t packet = f * d->pkt_count;
435
436     bd_seek(d->bd, packet * 192);
437     return 1;
438 }
439
440 int hb_bd_seek_pts( hb_bd_t * d, uint64_t pts )
441 {
442     bd_seek_time(d->bd, pts);
443     return 1;
444 }
445
446 int hb_bd_seek_chapter( hb_bd_t * d, int c )
447 {
448     int64_t pos;
449     d->next_chap = c;
450     pos = bd_seek_chapter( d->bd, c - 1 );
451     return 1;
452 }
453
454 /***********************************************************************
455  * hb_bd_read
456  ***********************************************************************
457  *
458  **********************************************************************/
459 int hb_bd_read( hb_bd_t * d, hb_buffer_t * b )
460 {
461     int result;
462     int error_count = 0;
463     uint8_t buf[192];
464     BD_EVENT event;
465     uint64_t pos;
466
467     while ( 1 )
468     {
469         if ( d->next_chap != d->chapter )
470         {
471             b->new_chap = d->chapter = d->next_chap;
472         }
473         result = next_packet( d->bd, buf );
474         if ( result < 0 )
475         {
476             hb_error("bd: Read Error");
477             pos = bd_tell( d->bd );
478             bd_seek( d->bd, pos + 192 );
479             error_count++;
480             if (error_count > 10)
481             {
482                 hb_error("bd: Error, too many consecutive read errors");
483                 return 0;
484             }
485             continue;
486         }
487         else if ( result == 0 )
488         {
489             return 0;
490         }
491
492         error_count = 0;
493         while ( bd_get_event( d->bd, &event ) )
494         {
495             switch ( event.event )
496             {
497                 case BD_EVENT_CHAPTER:
498                     // The muxers expect to only get chapter 2 and above
499                     // They write chapter 1 when chapter 2 is detected.
500                     d->next_chap = event.param;
501                     break;
502
503                 default:
504                     break;
505             }
506         }
507         // buf+4 to skip the BD timestamp at start of packet
508         result = hb_ts_decode_pkt( d->stream, buf+4, b );
509         if ( result )
510         {
511             return 1;
512         }
513     }
514     return 0;
515 }
516
517 /***********************************************************************
518  * hb_bd_chapter
519  ***********************************************************************
520  * Returns in which chapter the next block to be read is.
521  * Chapter numbers start at 1.
522  **********************************************************************/
523 int hb_bd_chapter( hb_bd_t * d )
524 {
525     return d->next_chap;
526 }
527
528 /***********************************************************************
529  * hb_bd_close
530  ***********************************************************************
531  * Closes and frees everything
532  **********************************************************************/
533 void hb_bd_close( hb_bd_t ** _d )
534 {
535     hb_bd_t * d = *_d;
536
537     if( d->stream ) hb_stream_close( &d->stream );
538     if( d->bd ) bd_close( d->bd );
539
540     free( d );
541     *_d = NULL;
542 }
543
544 /***********************************************************************
545  * hb_bd_set_angle
546  ***********************************************************************
547  * Sets the angle to read
548  **********************************************************************/
549 void hb_bd_set_angle( hb_bd_t * d, int angle )
550 {
551
552     if ( !bd_select_angle( d->bd, angle) )
553     {
554         hb_log("bd_select_angle failed");
555     }
556 }
557
558 static int check_ts_sync(const uint8_t *buf)
559 {
560     // must have initial sync byte, no scrambling & a legal adaptation ctrl
561     return (buf[0] == 0x47) && ((buf[3] >> 6) == 0) && ((buf[3] >> 4) > 0);
562 }
563
564 static int have_ts_sync(const uint8_t *buf, int psize)
565 {
566     return check_ts_sync(&buf[0*psize]) && check_ts_sync(&buf[1*psize]) &&
567            check_ts_sync(&buf[2*psize]) && check_ts_sync(&buf[3*psize]) &&
568            check_ts_sync(&buf[4*psize]) && check_ts_sync(&buf[5*psize]) &&
569            check_ts_sync(&buf[6*psize]) && check_ts_sync(&buf[7*psize]);
570 }
571
572 #define MAX_HOLE 192*80
573
574 static uint64_t align_to_next_packet(BLURAY *bd)
575 {
576     uint8_t buf[MAX_HOLE];
577     uint64_t pos = 0;
578     uint64_t start = bd_tell(bd);
579     uint64_t orig;
580
581     if ( start >= 192 ) {
582         start -= 192;
583         bd_seek(bd, start);
584     }
585     orig = start;
586
587     while (1)
588     {
589         if (bd_read(bd, buf, sizeof(buf)) == sizeof(buf))
590         {
591             const uint8_t *bp = buf;
592             int i;
593
594             for ( i = sizeof(buf) - 8 * 192; --i >= 0; ++bp )
595             {
596                 if ( have_ts_sync( bp, 192 ) )
597                 {
598                     break;
599                 }
600             }
601             if ( i >= 0 )
602             {
603                 pos = ( bp - buf );
604                 break;
605             }
606             uint64_t next = start + sizeof(buf) - 8 * 192;
607             bd_seek(bd, next);
608             start = bd_tell(bd);
609         }
610         else
611         {
612             return 0;
613         }
614     }
615     bd_seek(bd, start+pos);
616     return start - orig + pos;
617 }
618
619 static int next_packet( BLURAY *bd, uint8_t *pkt )
620 {
621     int result;
622
623     while ( 1 )
624     {
625         result = bd_read( bd, pkt, 192 );
626         if ( result < 0 )
627         {
628             return -1;
629         }
630         if ( result < 192 )
631         {
632             return 0;
633         }
634         // Sync byte is byte 4.  0-3 are timestamp.
635         if (pkt[4] == 0x47)
636         {
637             return 1;
638         }
639         // lost sync - back up to where we started then try to re-establish.
640         uint64_t pos = bd_tell(bd) - 192;
641         uint64_t pos2 = align_to_next_packet(bd);
642         if ( pos2 == 0 )
643         {
644             hb_log( "next_packet: eof while re-establishing sync @ %"PRId64, pos );
645             return 0;
646         }
647         hb_log( "next_packet: sync lost @ %"PRId64", regained after %"PRId64" bytes",
648                  pos, pos2 );
649     }
650 }
651