Index: libhb/hb.c =================================================================== --- libhb/hb.c (revision 1485) +++ libhb/hb.c (working copy) @@ -656,8 +656,9 @@ 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) Index: libhb/decavcodec.c =================================================================== --- libhb/decavcodec.c (revision 1485) +++ libhb/decavcodec.c (working copy) @@ -473,21 +473,24 @@ 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.num : 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; Index: libhb/scan.c =================================================================== --- libhb/scan.c (revision 1487) +++ libhb/scan.c (working copy) @@ -176,6 +176,13 @@ /* 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 @@ 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 @@ 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 @@ if ( vid_buf ) hb_buffer_close( &vid_buf ); } + + 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; + } - /* 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 ) ) { Index: libhb/common.h =================================================================== --- libhb/common.h (revision 1485) +++ libhb/common.h (working copy) @@ -437,6 +437,8 @@ 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 @@ 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