I have written a similar function and it accepts both source and destination pixel aspect ratio:
Code:
function parresize(clip c, int dst_w, int dst_h, string "dst_par", string "src_par", bool "ss")
{
unit_w = c.isRGB() ? 1 : 2
unit_h = c.isRGB() || c.isYUY2() ? 1 : 2
assert(dst_w % unit_w == 0, "Destination width must be divisible by " + string(unit_w))
assert(dst_h % unit_h == 0, "Destination height must be divisible by " + string(unit_h))
ss = default(ss, false)
src_par = default(src_par, "vga11")
dst_par = default(dst_par, "vga11")
cvt_par = \
( \
dst_par == "pal43" ? 12.0/11.0 : \
dst_par == "ntsc43" ? 10.0/11.0 : \
dst_par == "pal169" ? 16.0/11.0 : \
dst_par == "ntsc169" ? 40.0/33.0 : \
1.0 \
) / \
( \
src_par == "pal43" ? 12.0/11.0 : \
src_par == "ntsc43" ? 10.0/11.0 : \
src_par == "pal169" ? 16.0/11.0 : \
src_par == "ntsc169" ? 40.0/33.0 : \
1.0 \
)
src_dar = float(c.width) / cvt_par / c.height
dst_dar = float(dst_w) / dst_h
temp_w = floor((src_dar >= dst_dar ? dst_w : round(1.0 * dst_h * src_dar))/unit_w)*unit_w
temp_h = floor((src_dar >= dst_dar ? round(1.0 * dst_w / src_dar) : dst_h)/unit_h)*unit_h
r_factor = ss ? int(pow(2,ceil(log(max(float(temp_w)/c.width,float(temp_h)/c.height))/log(2.0)))) : 1
s1 = r_factor == 1 \
? c.blackmanresize(temp_w,temp_h) \
: c.nnedi3_rpow2(rfactor=r_factor,cshift="spline64resize",fwidth=temp_w,fheight=temp_h)
pad_l = floor(0.5*(dst_w-temp_w)/unit_w)*unit_w
pad_r = dst_w - pad_l - temp_w
pad_t = floor(0.5*(dst_h-temp_h)/unit_h)*unit_h
pad_b = dst_h - pad_t - temp_h
return s1.addborders(pad_l, pad_t, pad_r, pad_b)
}
Thanks Gavino for the suggestion.