diff options
Diffstat (limited to 'libhb')
-rw-r--r-- | libhb/common.h | 4 | ||||
-rw-r--r-- | libhb/decavcodec.c | 15 | ||||
-rw-r--r-- | libhb/hb.c | 5 | ||||
-rw-r--r-- | libhb/scan.c | 70 |
4 files changed, 63 insertions, 31 deletions
diff --git a/libhb/common.h b/libhb/common.h index d7728770e..0e531d922 100644 --- a/libhb/common.h +++ b/libhb/common.h @@ -437,6 +437,8 @@ struct hb_title_s int width; int height; int aspect; + int pixel_aspect_width; + int pixel_aspect_height; int rate; int rate_base; int crop[4]; @@ -521,6 +523,8 @@ typedef struct hb_work_info_s struct { // info only valid for video decoders int width; int height; + int pixel_aspect_width; + int pixel_aspect_height; double aspect; }; struct { // info only valid for audio decoders diff --git a/libhb/decavcodec.c b/libhb/decavcodec.c index dfa53e6a6..d050e39f9 100644 --- a/libhb/decavcodec.c +++ b/libhb/decavcodec.c @@ -473,21 +473,24 @@ static int decavcodecvInfo( hb_work_object_t *w, hb_work_info_t *info ) info->rate = 27000000; info->rate_base = (int64_t)context->time_base.num * 27000000LL / context->time_base.den; + + /* Sometimes there's no pixel aspect set in the source. In that case, + assume a 1:1 PAR. Otherwise, preserve the source PAR. */ + info->pixel_aspect_width = context->sample_aspect_ratio.num ? + context->sample_aspect_ratio.num : 1; + info->pixel_aspect_height = context->sample_aspect_ratio.den ? + context->sample_aspect_ratio.den : 1; /* ffmpeg returns the Pixel Aspect Ratio (PAR). Handbrake wants the * Display Aspect Ratio so we convert by scaling by the Storage * Aspect Ratio (w/h). We do the calc in floating point to get the * rounding right. We round in the second decimal digit because we * scale the (integer) aspect by 9 to preserve the 1st digit. */ - info->aspect = ( (double)context->sample_aspect_ratio.num * + info->aspect = ( (double)info->pixel_aspect_width * (double)context->width / - (double)context->sample_aspect_ratio.den / + (double)info->pixel_aspect_height / (double)context->height + 0.05 ) * HB_ASPECT_BASE; - if( context->sample_aspect_ratio.num == 0 ) - { - info->aspect = (double)context->width / (double)context->height * HB_ASPECT_BASE; - } info->profile = context->profile; info->level = context->level; info->name = context->codec->name; diff --git a/libhb/hb.c b/libhb/hb.c index bcd77e7a9..07efb88bb 100644 --- a/libhb/hb.c +++ b/libhb/hb.c @@ -656,8 +656,9 @@ void hb_set_anamorphic_size( hb_job_t * job, int pixel_aspect_width = job->pixel_aspect_width; int pixel_aspect_height = job->pixel_aspect_height; - - if (cropped_width <= 706) + + /* Only try to guess a pixel aspect if there isn't one set by the source.*/ + if (cropped_width <= 706 && !title->pixel_aspect_width && !title->pixel_aspect_height) { /* Handle ITU PARs */ if (title->height == 480) diff --git a/libhb/scan.c b/libhb/scan.c index 2a8d4437d..d66ccca42 100644 --- a/libhb/scan.c +++ b/libhb/scan.c @@ -176,6 +176,13 @@ static void ScanFunc( void * _data ) /* Autocrop by default. Gnark gnark */ memcpy( job->crop, title->crop, 4 * sizeof( int ) ); + /* Preserve a source's pixel aspect, if it's available. */ + if( title->pixel_aspect_width && title->pixel_aspect_height ) + { + job->pixel_aspect_width = title->pixel_aspect_width; + job->pixel_aspect_height = title->pixel_aspect_height; + } + if( title->aspect == 16 && !job->pixel_aspect_width && !job->pixel_aspect_height) { hb_reduce( &job->pixel_aspect_width, &job->pixel_aspect_height, @@ -345,6 +352,9 @@ static int DecodePreviews( hb_scan_t * data, hb_title_t * title ) title->width = vid_info.width; title->height = vid_info.height; + title->pixel_aspect_width = vid_info.pixel_aspect_width; + title->pixel_aspect_height = vid_info.pixel_aspect_height; + title->rate = vid_info.rate; title->rate_base = vid_info.rate_base; if ( vid_info.aspect != 0 ) @@ -354,21 +364,27 @@ static int DecodePreviews( hb_scan_t * data, hb_title_t * title ) hb_log( "aspect ratio changed from %g to %g", last_ar, vid_info.aspect ); } - switch ( (int)vid_info.aspect ) + + if( !title->pixel_aspect_width && !title->pixel_aspect_height ) { - case HB_ASPECT_BASE * 4 / 3: - ++ar4_count; - break; - case HB_ASPECT_BASE * 16 / 9: - ++ar16_count; - break; - default: - hb_log( "unknown aspect ratio %g", vid_info.aspect ); - /* if the aspect is closer to 4:3 use that - * otherwise use 16:9 */ - vid_info.aspect < HB_ASPECT_BASE * 14 / 9 ? ++ar4_count : - ++ar16_count; - break; + /* We don't have pixel aspect info from the source, so we're + going to have to make a guess on the display aspect ratio. */ + switch ( (int)vid_info.aspect ) + { + case HB_ASPECT_BASE * 4 / 3: + ++ar4_count; + break; + case HB_ASPECT_BASE * 16 / 9: + ++ar16_count; + break; + default: + hb_log( "unknown aspect ratio %g", vid_info.aspect ); + /* if the aspect is closer to 4:3 use that + * otherwise use 16:9 */ + vid_info.aspect < HB_ASPECT_BASE * 14 / 9 ? ++ar4_count : + ++ar16_count; + break; + } } last_ar = vid_info.aspect; } @@ -489,23 +505,31 @@ skip_preview: if ( vid_buf ) hb_buffer_close( &vid_buf ); } - - /* if we found mostly 4:3 previews use that as the aspect ratio otherwise - use 16:9 */ - title->aspect = ar4_count > ar16_count ? - HB_ASPECT_BASE * 4 / 3 : HB_ASPECT_BASE * 16 / 9; + + if( title->pixel_aspect_width && title->pixel_aspect_width ) + { + title->aspect = ( (double)title->pixel_aspect_width * + (double)title->width / + (double)title->pixel_aspect_height / + (double)title->height + 0.05 ) * HB_ASPECT_BASE; + } + else + { + /* if we found mostly 4:3 previews use that as the aspect ratio otherwise + use 16:9 */ + title->aspect = ar4_count > ar16_count ? + HB_ASPECT_BASE * 4 / 3 : HB_ASPECT_BASE * 16 / 9; + } title->crop[0] = EVEN( title->crop[0] ); title->crop[1] = EVEN( title->crop[1] ); title->crop[2] = EVEN( title->crop[2] ); title->crop[3] = EVEN( title->crop[3] ); - hb_log( "scan: %d previews, %dx%d, %.3f fps, autocrop = %d/%d/%d/%d, aspect %s", + hb_log( "scan: %d previews, %dx%d, %.3f fps, autocrop = %d/%d/%d/%d, aspect %.2f", npreviews, title->width, title->height, (float) title->rate / (float) title->rate_base, title->crop[0], title->crop[1], - title->crop[2], title->crop[3], - title->aspect == HB_ASPECT_BASE * 16 / 9 ? "16:9" : - title->aspect == HB_ASPECT_BASE * 4 / 3 ? "4:3" : "none" ); + title->crop[2], title->crop[3], (float)title->aspect/(float)HB_ASPECT_BASE); if( interlaced_preview_count >= ( npreviews / 2 ) ) { |