1 /* $Id: scan.c,v 1.52 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. */
8 #include "a52dec/a52.h"
11 #define HB_MAX_PREVIEWS 30 // 30 previews = every 5 minutes of a 2.5 hour video
19 hb_list_t * list_title;
29 static void ScanFunc( void * );
30 static int DecodePreviews( hb_scan_t *, hb_title_t * title );
31 static void LookForAudio( hb_title_t * title, hb_buffer_t * b );
32 static int AllAudioOK( hb_title_t * title );
34 static const char *aspect_to_string( double aspect )
36 switch ( (int)(aspect * 9.) )
38 case 9 * 4 / 3: return "4:3";
39 case 9 * 16 / 9: return "16:9";
41 static char arstr[32];
42 sprintf( arstr, aspect >= 1.? "%.2f:1" : "1:%.2f", aspect );
46 hb_thread_t * hb_scan_init( hb_handle_t * handle, const char * path,
47 int title_index, hb_list_t * list_title,
48 int preview_count, int store_previews )
50 hb_scan_t * data = calloc( sizeof( hb_scan_t ), 1 );
53 data->path = strdup( path );
54 data->title_index = title_index;
55 data->list_title = list_title;
57 data->preview_count = preview_count;
58 data->store_previews = store_previews;
60 return hb_thread_init( "scan", ScanFunc, data, HB_NORMAL_PRIORITY );
63 static void ScanFunc( void * _data )
65 hb_scan_t * data = (hb_scan_t *) _data;
72 /* Try to open the path as a DVD. If it fails, try as a file */
73 hb_log( "scan: trying to open with libdvdread" );
74 if( ( data->dvd = hb_dvd_init( data->path ) ) )
76 hb_log( "scan: DVD has %d title(s)",
77 hb_dvd_title_count( data->dvd ) );
78 if( data->title_index )
80 /* Scan this title only */
81 hb_list_add( data->list_title, hb_dvd_title_scan( data->dvd,
82 data->title_index ) );
87 for( i = 0; i < hb_dvd_title_count( data->dvd ); i++ )
89 hb_list_add( data->list_title,
90 hb_dvd_title_scan( data->dvd, i + 1 ) );
94 else if ( (data->stream = hb_stream_open( data->path, 0 ) ) != NULL )
96 hb_list_add( data->list_title, hb_stream_title_scan( data->stream ) );
100 hb_log( "scan: unrecognized file type" );
104 for( i = 0; i < hb_list_count( data->list_title ); )
110 title = hb_list_item( data->list_title, i );
112 #define p state.param.scanning
114 state.state = HB_STATE_SCANNING;
115 p.title_cur = title->index;
116 p.title_count = data->dvd ? hb_dvd_title_count( data->dvd ) : hb_list_count(data->list_title);
117 hb_set_state( data->h, &state );
120 /* Decode previews */
121 /* this will also detect more AC3 / DTS information */
122 if( !DecodePreviews( data, title ) )
124 /* TODO: free things */
125 hb_list_rem( data->list_title, title );
129 /* Make sure we found audio rates and bitrates */
130 for( j = 0; j < hb_list_count( title->list_audio ); )
132 audio = hb_list_item( title->list_audio, j );
133 if( !audio->config.in.bitrate )
135 hb_log( "scan: removing audio 0x%x because no bitrate found",
137 hb_list_rem( title->list_audio, audio );
147 /* Init jobs templates */
148 for( i = 0; i < hb_list_count( data->list_title ); i++ )
152 title = hb_list_item( data->list_title, i );
153 job = calloc( sizeof( hb_job_t ), 1 );
158 /* Set defaults settings */
159 job->chapter_start = 1;
160 job->chapter_end = hb_list_count( title->list_chapter );
162 /* Autocrop by default. Gnark gnark */
163 memcpy( job->crop, title->crop, 4 * sizeof( int ) );
165 /* Preserve a source's pixel aspect, if it's available. */
166 if( title->pixel_aspect_width && title->pixel_aspect_height )
168 job->anamorphic.par_width = title->pixel_aspect_width;
169 job->anamorphic.par_height = title->pixel_aspect_height;
172 if( title->aspect != 0 && title->aspect != 1. &&
173 !job->anamorphic.par_width && !job->anamorphic.par_height)
175 hb_reduce( &job->anamorphic.par_width, &job->anamorphic.par_height,
176 (int)(title->aspect * title->height + 0.5), title->width );
179 job->width = title->width - job->crop[2] - job->crop[3];
180 hb_fix_aspect( job, HB_KEEP_WIDTH );
181 if( job->height > title->height - job->crop[0] - job->crop[1] )
183 job->height = title->height - job->crop[0] - job->crop[1];
184 hb_fix_aspect( job, HB_KEEP_HEIGHT );
187 hb_log( "scan: title (%d) job->width:%d, job->height:%d",
188 i, job->width, job->height );
192 job->vcodec = HB_VCODEC_FFMPEG;
193 job->vquality = -1.0;
194 job->vbitrate = 1000;
196 job->vrate = title->rate;
197 job->vrate_base = title->rate_base;
199 job->list_audio = hb_list_init();
200 job->list_subtitle = hb_list_init();
202 job->mux = HB_MUX_MP4;
207 hb_dvd_close( &data->dvd );
211 hb_stream_close(&data->stream);
218 // -----------------------------------------------
219 // stuff related to cropping
223 static inline int absdiff( int x, int y )
225 return x < y ? y - x : x - y;
228 static inline int clampBlack( int x )
230 // luma 'black' is 16 and anything less should be clamped at 16
231 return x < 16 ? 16 : x;
234 static int row_all_dark( hb_title_t *title, uint8_t* luma, int row )
236 luma += title->width * row;
238 // compute the average luma value of the row
240 for ( i = 0; i < title->width; ++i )
242 avg += clampBlack( luma[i] );
248 // since we're trying to detect smooth borders, only take the row if
249 // all pixels are within +-16 of the average (this range is fairly coarse
250 // but there's a lot of quantization noise for luma values near black
251 // so anything less will fail to crop because of the noise).
252 for ( i = 0; i < title->width; ++i )
254 if ( absdiff( avg, clampBlack( luma[i] ) ) > 16 )
260 static int column_all_dark( hb_title_t *title, uint8_t* luma, int top, int bottom,
263 int stride = title->width;
264 int height = title->height - top - bottom;
265 luma += stride * top + col;
267 // compute the average value of the column
268 int i = height, avg = 0, row = 0;
269 for ( ; --i >= 0; row += stride )
271 avg += clampBlack( luma[row] );
277 // since we're trying to detect smooth borders, only take the column if
278 // all pixels are within +-16 of the average.
280 for ( ; --i >= 0; row += stride )
282 if ( absdiff( avg, clampBlack( luma[row] ) ) > 16 )
291 int t[HB_MAX_PREVIEWS];
292 int b[HB_MAX_PREVIEWS];
293 int l[HB_MAX_PREVIEWS];
294 int r[HB_MAX_PREVIEWS];
297 static void record_crop( crop_record_t *crops, int t, int b, int l, int r )
299 crops->t[crops->n] = t;
300 crops->b[crops->n] = b;
301 crops->l[crops->n] = l;
302 crops->r[crops->n] = r;
306 static int compare_int( const void *a, const void *b )
308 return *(const int *)a - *(const int *)b;
311 static void sort_crops( crop_record_t *crops )
313 qsort( crops->t, crops->n, sizeof(crops->t[0]), compare_int );
314 qsort( crops->b, crops->n, sizeof(crops->t[0]), compare_int );
315 qsort( crops->l, crops->n, sizeof(crops->t[0]), compare_int );
316 qsort( crops->r, crops->n, sizeof(crops->t[0]), compare_int );
319 // -----------------------------------------------
320 // stuff related to title width/height/aspect info
323 int count; /* number of times we've seen this info entry */
324 hb_work_info_t info; /* copy of info entry */
327 static void remember_info( info_list_t *info_list, hb_work_info_t *info )
329 for ( ; info_list->count; ++info_list )
331 if ( memcmp( &info_list->info, info, sizeof(*info) ) == 0 )
333 // we found a match - bump its count
338 // no match found - add new entry to list (info_list points to
339 // the first free slot). NB - we assume that info_list was allocated
340 // so that it's big enough even if there are no dups. I.e., 10 slots
341 // allocated if there are 10 previews.
342 info_list->count = 1;
343 info_list->info = *info;
346 static void most_common_info( info_list_t *info_list, hb_work_info_t *info )
349 for ( i = 1; info_list[i].count; ++i )
351 if ( info_list[i].count > info_list[biggest].count )
354 *info = info_list[biggest].info;
358 /***********************************************************************
360 ***********************************************************************
361 * Decode 10 pictures for the given title.
362 * It assumes that data->reader and data->vts have successfully been
363 * DVDOpen()ed and ifoOpen()ed.
364 **********************************************************************/
365 static int DecodePreviews( hb_scan_t * data, hb_title_t * title )
367 int i, npreviews = 0;
368 hb_buffer_t * buf_ps, * buf_es;
370 int progressive_count = 0;
371 int interlaced_preview_count = 0;
372 info_list_t * info_list = calloc( data->preview_count+1, sizeof(*info_list) );
373 crop_record_t *crops = calloc( 1, sizeof(*crops) );
375 buf_ps = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
376 list_es = hb_list_init();
378 hb_log( "scan: decoding previews for title %d", title->index );
382 hb_dvd_start( data->dvd, title, 1 );
383 title->angle_count = hb_dvd_angle_count( data->dvd );
384 hb_log( "scan: title angle(s) %d", title->angle_count );
387 for( i = 0; i < data->preview_count; i++ )
395 if( !hb_dvd_seek( data->dvd, (float) ( i + 1 ) / ( data->preview_count + 1.0 ) ) )
400 else if (data->stream)
402 /* we start reading streams at zero rather than 1/11 because
403 * short streams may have only one sequence header in the entire
404 * file and we need it to decode any previews. */
405 if (!hb_stream_seek(data->stream, (float) i / ( data->preview_count + 1.0 ) ) )
411 hb_deep_log( 2, "scan: preview %d", i + 1 );
413 int vcodec = title->video_codec? title->video_codec : WORK_DECMPEG2;
414 hb_work_object_t *vid_decoder = hb_get_work( vcodec );
415 vid_decoder->codec_param = title->video_codec_param;
416 vid_decoder->title = title;
417 vid_decoder->init( vid_decoder, NULL );
418 hb_buffer_t * vid_buf = NULL;
421 if ( title->flags & HBTF_NO_IDR )
423 // title doesn't have IDR frames so we decode but drop one second's
424 // worth of frames to allow the decoder to converge.
425 if ( ! title->rate_base )
431 vidskip = (double)title->rate / (double)title->rate_base + 0.5;
435 for( j = 0; j < 10240 ; j++ )
439 if( !hb_dvd_read( data->dvd, buf_ps ) )
445 hb_log( "Warning: Could not read data for preview %d, skipped", i + 1 );
449 else if (data->stream)
451 if ( !hb_stream_read(data->stream,buf_ps) )
457 hb_log( "Warning: Could not read data for preview %d, skipped", i + 1 );
461 (hb_demux[title->demuxer])(buf_ps, list_es, 0 );
463 while( ( buf_es = hb_list_item( list_es, 0 ) ) )
465 hb_list_rem( list_es, buf_es );
466 if( buf_es->id == title->video_id && vid_buf == NULL )
468 vid_decoder->work( vid_decoder, &buf_es, &vid_buf );
469 if ( vid_buf && vidskip && --vidskip > 0 )
471 // we're dropping frames to get the video decoder in sync
472 // when the video stream doesn't contain IDR frames
473 hb_buffer_close( &vid_buf );
477 else if( ! AllAudioOK( title ) )
479 LookForAudio( title, buf_es );
482 hb_buffer_close( &buf_es );
485 if( vid_buf && AllAudioOK( title ) )
491 hb_log( "scan: could not get a decoded picture" );
495 /* Get size and rate infos */
497 hb_work_info_t vid_info;
498 if( !vid_decoder->info( vid_decoder, &vid_info ) )
501 * Could not fill vid_info, don't continue and try to use vid_info
504 vid_decoder->close( vid_decoder );
508 vid_decoder->close( vid_decoder );
511 remember_info( info_list, &vid_info );
513 title->video_codec_name = strdup( vid_info.name );
514 title->width = vid_info.width;
515 title->height = vid_info.height;
516 title->rate = vid_info.rate;
517 title->rate_base = vid_info.rate_base;
518 title->video_bitrate = vid_info.bitrate;
520 if( title->rate_base == 1126125 )
522 /* Frame FPS is 23.976 (meaning it's progressive), so
523 start keeping track of how many are reporting at
524 that speed. When enough show up that way, we want
525 to make that the overall title FPS.
529 if( progressive_count < 6 )
531 /* Not enough frames are reporting as progressive,
532 which means we should be conservative and use
533 29.97 as the title's FPS for now.
535 title->rate_base = 900900;
539 /* A majority of the scan frames are progressive. Make that
540 the title's FPS, and announce it once to the log.
542 if( progressive_count == 6 )
544 hb_deep_log( 2, "Title's mostly NTSC Film, setting fps to 23.976");
546 title->rate_base = 1126125;
549 else if( title->rate_base == 900900 && progressive_count >= 6 )
552 * We've already deduced that the frame rate is 23.976, so set it
555 title->rate_base = 1126125;
558 while( ( buf_es = hb_list_item( list_es, 0 ) ) )
560 hb_list_rem( list_es, buf_es );
561 hb_buffer_close( &buf_es );
564 /* Check preview for interlacing artifacts */
565 if( hb_detect_comb( vid_buf, title->width, title->height, 10, 30, 9, 10, 30, 9 ) )
567 hb_deep_log( 2, "Interlacing detected in preview frame %i", i+1);
568 interlaced_preview_count++;
571 if( data->store_previews )
573 hb_get_tempory_filename( data->h, filename, "%" PRIxPTR "%d",
574 (intptr_t)title, i );
576 file_preview = fopen( filename, "wb" );
579 fwrite( vid_buf->data, title->width * title->height * 3 / 2,
581 fclose( file_preview );
585 hb_log( "scan: fopen failed (%s)", filename );
589 /* Detect black borders */
591 #define Y vid_buf->data
592 int top, bottom, left, right;
593 int h4 = title->height / 4, w4 = title->width / 4;
595 // When widescreen content is matted to 16:9 or 4:3 there's sometimes
596 // a thin border on the outer edge of the matte. On TV content it can be
597 // "line 21" VBI data that's normally hidden in the overscan. For HD
598 // content it can just be a diagnostic added in post production so that
599 // the frame borders are visible. We try to ignore these borders so
600 // we can crop the matte. The border width depends on the resolution
601 // (12 pixels on 1080i looks visually the same as 4 pixels on 480i)
602 // so we allow the border to be up to 1% of the frame height.
603 const int border = title->height / 100;
605 for ( top = border; top < h4; ++top )
607 if ( ! row_all_dark( title, Y, top ) )
612 // we never made it past the border region - see if the rows we
613 // didn't check are dark or if we shouldn't crop at all.
614 for ( top = 0; top < border; ++top )
616 if ( ! row_all_dark( title, Y, top ) )
624 for ( bottom = border; bottom < h4; ++bottom )
626 if ( ! row_all_dark( title, Y, title->height - 1 - bottom ) )
629 if ( bottom <= border )
631 for ( bottom = 0; bottom < border; ++bottom )
633 if ( ! row_all_dark( title, Y, title->height - 1 - bottom ) )
636 if ( bottom >= border )
641 for ( left = 0; left < w4; ++left )
643 if ( ! column_all_dark( title, Y, top, bottom, left ) )
646 for ( right = 0; right < w4; ++right )
648 if ( ! column_all_dark( title, Y, top, bottom, title->width - 1 - right ) )
652 // only record the result if all the crops are less than a quarter of
653 // the frame otherwise we can get fooled by frames with a lot of black
654 // like titles, credits & fade-thru-black transitions.
655 if ( top < h4 && bottom < h4 && left < w4 && right < w4 )
657 record_crop( crops, top, bottom, left, right );
663 hb_buffer_close( &vid_buf );
668 // use the most common frame info for our final title dimensions
669 hb_work_info_t vid_info;
670 most_common_info( info_list, &vid_info );
672 title->width = vid_info.width;
673 title->height = vid_info.height;
674 title->pixel_aspect_width = vid_info.pixel_aspect_width;
675 title->pixel_aspect_height = vid_info.pixel_aspect_height;
677 // compute the aspect ratio based on the storage dimensions and the
678 // pixel aspect ratio (if supplied) or just storage dimensions if no PAR.
679 title->aspect = (double)title->width / (double)title->height;
680 if( title->pixel_aspect_width && title->pixel_aspect_height )
682 title->aspect *= (double)title->pixel_aspect_width /
683 (double)title->pixel_aspect_height;
685 // For unknown reasons some French PAL DVDs put the original
686 // content's aspect ratio into the mpeg PAR even though it's
687 // the wrong PAR for the DVD. Apparently they rely on the fact
688 // that DVD players ignore the content PAR and just use the
689 // aspect ratio from the DVD metadata. So, if the aspect computed
690 // from the PAR is different from the container's aspect we use
691 // the container's aspect & recompute the PAR from it.
692 if( title->container_aspect && (int)(title->aspect * 9) != (int)(title->container_aspect * 9) )
694 hb_log("scan: content PAR gives wrong aspect %.2f; "
695 "using container aspect %.2f", title->aspect,
696 title->container_aspect );
697 title->aspect = title->container_aspect;
698 hb_reduce( &title->pixel_aspect_width, &title->pixel_aspect_height,
699 (int)(title->aspect * title->height + 0.5), title->width );
703 // don't try to crop unless we got at least 3 previews
707 // The next line selects median cropping - at least
708 // 50% of the frames will have their borders removed.
709 // Other possible choices are loose cropping (i = 0) where
710 // no non-black pixels will be cropped from any frame and a
711 // tight cropping (i = crops->n - (crops->n >> 2)) where at
712 // least 75% of the frames will have their borders removed.
714 title->crop[0] = EVEN( crops->t[i] );
715 title->crop[1] = EVEN( crops->b[i] );
716 title->crop[2] = EVEN( crops->l[i] );
717 title->crop[3] = EVEN( crops->r[i] );
721 hb_log( "scan: %d previews, %dx%d, %.3f fps, autocrop = %d/%d/%d/%d, "
722 "aspect %s, PAR %d:%d",
723 npreviews, title->width, title->height, (float) title->rate /
724 (float) title->rate_base,
725 title->crop[0], title->crop[1], title->crop[2], title->crop[3],
726 aspect_to_string( title->aspect ), title->pixel_aspect_width,
727 title->pixel_aspect_height );
729 if( interlaced_preview_count >= ( npreviews / 2 ) )
731 hb_log("Title is likely interlaced or telecined (%i out of %i previews). You should do something about that.",
732 interlaced_preview_count, npreviews);
733 title->detected_interlacing = 1;
737 title->detected_interlacing = 0;
741 hb_buffer_close( &buf_ps );
742 while( ( buf_es = hb_list_item( list_es, 0 ) ) )
744 hb_list_rem( list_es, buf_es );
745 hb_buffer_close( &buf_es );
747 hb_list_close( &list_es );
749 hb_dvd_stop( data->dvd );
755 * This routine is called for every frame from a non-video elementary stream.
756 * These are a mix of audio & subtitle streams, some of which we want & some
757 * we're ignoring. This routine checks the frame against all our audio streams
758 * to see if it's one we want and haven't identified yet. If yes, it passes the
759 * frame to a codec-specific id routine which is responsible for filling in
760 * the sample rate, bit rate, channels & other audio parameters.
762 * Since a sample rate is essential for further audio processing, any audio
763 * stream which isn't successfully id'd by is deleted at the end of the scan.
764 * This is necessary to avoid ambiguities where things that might be audio
765 * aren't (e.g., some European DVD Teletext streams use the same IDs as US ATSC
768 static void LookForAudio( hb_title_t * title, hb_buffer_t * b )
772 hb_audio_t * audio = NULL;
773 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
775 audio = hb_list_item( title->list_audio, i );
776 /* check if this elementary stream is one we want */
777 if ( audio->id == b->id )
786 if( !audio || audio->config.in.bitrate != 0 )
788 /* not found or already done */
792 hb_work_object_t *w = hb_codec_decoder( audio->config.in.codec );
794 if ( w == NULL || w->bsinfo == NULL )
796 hb_log( "Internal error in scan: unhandled audio type %d for id 0x%x",
797 audio->config.in.codec, audio->id );
803 w->codec_param = audio->config.in.codec_param;
804 int ret = w->bsinfo( w, b, &info );
807 hb_log( "no info on audio type %d/0x%x for id 0x%x",
808 audio->config.in.codec, audio->config.in.codec_param,
814 /* didn't find any info */
817 audio->config.in.samplerate = info.rate;
818 audio->config.in.bitrate = info.bitrate;
819 audio->config.in.channel_layout = info.channel_layout;
820 audio->config.in.version = info.version;
821 audio->config.in.mode = info.mode;
822 audio->config.flags.ac3 = info.flags;
824 // update the audio description string based on the info we found
825 if ( audio->config.flags.ac3 & AUDIO_F_DOLBY )
827 strcat( audio->config.lang.description, " (Dolby Surround)" );
831 int layout = audio->config.in.channel_layout;
832 char *desc = audio->config.lang.description +
833 strlen( audio->config.lang.description );
834 sprintf( desc, " (%d.%d ch)",
835 HB_INPUT_CH_LAYOUT_GET_DISCRETE_FRONT_COUNT(layout) +
836 HB_INPUT_CH_LAYOUT_GET_DISCRETE_REAR_COUNT(layout),
837 HB_INPUT_CH_LAYOUT_GET_DISCRETE_LFE_COUNT(layout) );
840 hb_log( "scan: audio 0x%x: %s, rate=%dHz, bitrate=%d %s", audio->id,
841 info.name, audio->config.in.samplerate, audio->config.in.bitrate,
842 audio->config.lang.description );
847 // We get here if there's no hope of finding info on an audio bitstream,
848 // either because we don't have a decoder (or a decoder with a bitstream
849 // info proc) or because the decoder's info proc said that the stream
850 // wasn't something it could handle. Delete the item from the title's
851 // audio list so we won't keep reading packets while trying to get its
857 hb_list_rem( title->list_audio, audio );
861 * This routine checks to see if we've ID'd all the audio streams associated
862 * with a title. It returns 0 if there are more to ID & 1 if all are done.
864 static int AllAudioOK( hb_title_t * title )
869 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
871 audio = hb_list_item( title->list_audio, i );
872 if( audio->config.in.bitrate == 0 )