Log in

View Full Version : Recomendation for Downsizer required - SOLVED.


StainlessS
4th April 2017, 08:18
Hi guys,

I'm wondering if there is any consensus on a better resizer for downsizing ?

This pretty much seems to work ok, but would like recommendation for the actual downsizer (I guess might be used for upsize too).


# ResizePadded.avs

Function ResizePadded(clip clp,Int W, Int H,
\ Int "xMod",Int "yMod",
\ Float "Bicub_b",Float "Bicub_c",
\ Int "src_left", Int "src_top", Int "src_width", Int "src_height",
\ Bool "Pad_Rec601"
\ ) {
/*
https://forum.doom9.org/showthread.php?t=174496
Req RT_Stats, mt_Tools v2.0.

Intended for downsize, where return clip dimensions are modulo xMod,yMod but some of result frame (right and bottom) may be padding,
ie if resize to W=29,H=29, and xMod=4,yMod=4, then frame will be 32x32, x=0,y=0,w=29,h=29 is valid graphic,
but rightmost and bottom most 3 pixels are padding only.
For use where mask used to apply valid graphic using eg Overlay.
Any Padding area will be returned as BLACK (0 for RGB or Y of YUV [not rec601 16 unless Pad_Rec601==True]).

Args:-
W,H Output size for return clip
Same colorspace as input clip, output may be Right and Bottom padded to Black, to comply with xMod,yMod requirement.

xMod, Default = natural colorspace alignment requirement of input and output clips,
yMod eg for YV411:- xMod=4,yMod=1. YV12:- xMod=yMod=2. YV16/YUY2:- xMod=2,yMod=1. Y8/YV24/RGB/24/32:- xMod=yMod=1.

Bicub_b, BicubicResize() b and c args. (If supplied as args, BEST SUPPLY BOTH Bicub_b and Bicub_c)
Bicub_c Default:-
DownSize b = -0.5, c = 0.25, Groucho2004,Didée:- https://forum.doom9.org/showthread.php?p=1802716#post1802716
Upsize, b = 0.0, c = 0.5, Catmull-Rom spline, sharp.
Avisynth BicubicResize() defaults are:- b=1.0/3, c=1.0/3

src_left, Source clip area to resize, all default 0, meaning full frame, as in Crop(0,0,0,0).
src_top,
src_width,
src_height

Pad_Rec601 Default False.
We normally return any YUV padding as Black Y=0, will change to padding Y = 16 if this set true.
NOTE, If using this function for resizing mask for use with overlay, you are UNLIKELY to
want to change this to True (you may get ghosts in your overlays).
If resizing a graphic intended for Overlay clip (together with a mask), then it probably makes little/no
difference whether you set Pad_rec601 to true or false.
RGB padding is always returned as Black $000000.
*/
clp myName="ResizePadded: "
DefXmod = RT_ColorSpaceXMod DefYmod = RT_ColorSpaceYMod(Laced=False) # Natural ColorSpace alignment for input clip
xMod=Default(xMod,DefXmod) yMod=Default(yMod,DefYmod)
Assert(0 < xMod && xMod % DefXmod == 0, RT_String("%sIllegal xMod(%d, DefXMod=%d)",myName,xMod,DefXmod))
Assert(0 < yMod && yMod % DefYmod == 0, RT_String("%sIllegal yMod(%d, DefYMod=%d)",myName,yMod,DefYMod))
Bicub_b = Default(Bicub_b, (W<clp.Width) ? -0.5 : 0.0)
Bicub_c = Default(Bicub_c, (W<clp.Width) ? 0.25 : 0.5)
src_left = Default(src_left,0) src_top = Default(src_top,0) src_width = Default(src_width,0) src_height = Default(src_height,0)
Pad_Rec601 = Default(Pad_Rec601,False)
Assert(0 <= src_left < Width, RT_string("%s0 <= src_left(%d) < Width(%d)",myName,src_left,Width))
Assert(0 <= src_top < Height, RT_string("%s0 <= src_top(%d) < Height(%d)",myName,src_top,Height))
src_width = (src_width <= 0) ? width - src_left + src_width : src_width
src_height= (src_height <= 0) ? height - src_top + src_height : src_height
Assert(0 < src_width <= Width, RT_String("%s0 < src_width(%d) <= Width(%d)",myName,src_width,Width))
Assert(0 < src_height <= Height, RT_String("%s0 < src_height(%d) <= Height(%d)",myName,src_height,Height))
CanvasW=(W+xMod-1)/xMod*xMod CanvasH=(H+yMod-1)/yMod*yMod # O/P frame size (rounded up to next multiple of xMod,yMod if necessary)
iPadR=CanvasW-W iPadB=CanvasH-H # O/P padding pixels (Right and Bottom)
fPadR=Float(iPadR)*src_width/W fPadB=Float(iPadB)*src_height/H # Fractional pixels to reach into source clip to achieve padding.
BicubicResize(CanvasW, CanvasH, b=Bicub_b, c=Bicub_c ,
\ src_left = src_left, src_top = src_top,
\ src_width = src_width+fPadR, src_height = src_height+fPadB
\ )
# Padding to BLACK : Cannot use LetterBox for YUV as will be set to Y=16 not Y=0 [Unless Pad_Rec601=True, then OK].
Last = (0 == iPadR == iPadB) ? Last
\ : (IsRGB() || Pad_Rec601) ? Letterbox(0,iPadB,0,iPadR)
\ : mt_merge(Last,
\ Last.BlankClip(Color_Yuv=$008080),
\ Last.mt_lutspa(relative=false,expr=RT_String("x %d >= y %d >= | 255 0 ?",W,H),chroma="-128"),luma=true)
Return Last
}



EDIT: I'm quite happy to take the first suggestion (If wrong, I can blame it on someone else :) )
https://www.cosgan.de/images/smilie/set/001.gifhttps://www.cosgan.de/images/smilie/set/020.gifhttps://www.cosgan.de/images/smilie/set/016.gifhttps://www.cosgan.de/images/smilie/set/013.gifhttps://www.cosgan.de/images/smilie/set/023.gifhttps://www.cosgan.de/images/smilie/set/006.gifhttps://www.cosgan.de/images/smilie/set/005.gif

Groucho2004
4th April 2017, 08:33
You're going to get a bunch of opinions, everyone has their own personal favourite. Here's mine:
BicubicResize(w, h, b = -0.5, c = 0.25)

This was inspired by Didée, see this (https://forum.doom9.org/showthread.php?p=1748922#post1748922) post.

Also, see this thread (https://forum.doom9.org/showthread.php?t=154583)

StainlessS
4th April 2017, 08:43
Thanx G2K4, that is good enough for me (and I dont get the blame either he he).

I'll leave this discussion now and let you all chat over it yourselves. :)

EDIT: Modded 1st post code, marked as SOLVED (but dont let that stop you-all discussing it).

EDIT: Modded to use suggested for downsize on width, or Catmull-Rom spline, (sharp) for upsize on width.

Gser
4th April 2017, 17:05
https://github.com/mysteryx93/AviSynthShader

ResizeShader(Input, Width, Height, Kernel, B, C, MatrixIn, MatrixOut, FormatOut, Convert, ConvertYuv, lsb_in, lsb_out, PlanarIn, PlanarOut)

using SSim as Kernel. I suppose if you wanted to be anal about it you could use it in junction with DitherTools.

StainlessS
4th April 2017, 20:31
Thank you Gser, but my requirement is now totally fulfilled.
Perhaps you would like to tell G2K4 how much better your suggestion is than his, and why he should change his habit. https://www.cosgan.de/images/midi/ekelig/e010.gif

StainlessS
5th April 2017, 14:13
Added BicubicResize() optional Bicub_b and Bicub_c args to first post script, probably no further alterations.

Best supply both or none.

age
6th April 2017, 13:35
theoretically mitchell bicubic follows the "b+2c=1" formula, b=c=1/3 ==> (1/3) + 2*(1/3)=1 but in computer programming 1/3=0.3333333 is an approximation and b+2c=0.9999999
so we can force b+2c=1 with truncated b/c parameters:
b =0.33333334 c=1/3=0.33333333 ==> b+2c=1

StainlessS
6th April 2017, 14:40
Function ResizePadded(clip clp,Int W, Int H,Int "xMod",Int "yMod",Float "Bicub_b",Float "Bicub_c") {
# https://forum.doom9.org/showthread.php?t=174496
# Intended for downsize, where return clip dimensions are modulo xMod,yMod but some of result frame (right and bottom) may be padding,
# ie if resize to W=29,H=29, and xMod=4,yMod=4, then frame will be 32x32, x=0,y=0,w=29,h=29 is valid graphic,
# but rightmost and bottom most 3 pixels are padding only. For use where mask used to apply valid graphic using eg Overlay.
xMod=Default(xMod,2) yMod=Default(yMod,2)
# DEFAULT:- BicubicResize b and c. (If given as args, BEST SUPPLY BOTH Bicub_b and Bicub_c)
# DownSize b = -0.5, c = 0.25, Groucho2004,Didée:- https://forum.doom9.org/showthread.php?p=1802716#post1802716
# Upsize, b = 0.0, c = 0.5, Catmull-Rom spline, sharp.
# Avisynth defaults are:- b=1.0/3, c=1.0/3
Bicub_b = Default(Bicub_b, (W<clp.Width) ? -0.5 : 0.0)
Bicub_c = Default(Bicub_c, (W<clp.Width) ? 0.25 : 0.5)
CanvasW=(W+xMod-1)/xMod*xMod CanvasH=(H+yMod-1)/yMod*yMod
dAddX=CanvasW-W dAddY=CanvasH-H
PadX=Float(dAddX)*clp.Width/W PadY=Float(dAddY)*clp.Height/H
clp.BicubicResize(CanvasW, CanvasH, b=Bicub_b, c=Bicub_c , src_left=0, src_top=0, src_width=clp.width+PadX, src_height=clp.height+PadY)
Return Last
}

# Return Clip Difference of input clips (amp==true = Amplified, show==true = show background)
Function ClipDelta(clip clip1,clip clip2,bool "amp",bool "show") {
amp=Default(amp,false)
show=Default(show,false)
c2=clip1.levels(128-32,1.0,128+32,128-32,128+32).greyscale()
c1=clip1.subtract(clip2)
c1=(amp)?c1.levels(127,1.0,129,0,255):c1
return (show)?c1.Merge(c2):c1
}

# From Visual Studio 10, Float.h
# #define FLT_DIG 6 /* # of decimal digits of precision */

AvisourcE("F:\V\XMEN2.avi")
O=Last

A=O.ResizePadded(640,480,Bicub_b=1.0/3,Bicub_c=1.0/3)
#B=O.ResizePadded(640,480,Bicub_b=0.33333334,Bicub_c=0.33333333) # Posted, signif digits=8
#B=O.ResizePadded(640,480,Bicub_b=0.333334,Bicub_c=0.333333) # Seems identical, signif digits=6
B=O.ResizePadded(640,480,Bicub_b=0.33334,Bicub_c=0.33333) # visible difference (just, when amplified), signif digits=5)

D1=ClipDelta(A,B)
D2=ClipDelta(A,B,True)
TOP=StackHorizontal(A,B)
BOT=StackHorizontal(D1,D2)
Stackvertical(TOP,BOT)


EDIT

B=O.ResizePadded(640,480,Bicub_b=0.333330,Bicub_c=0.333339) # signif digits=6, Occasional difference seen, when AMP


#B=O.ResizePadded(640,480,Bicub_b=0.333334,Bicub_c=0.333333) # Seems identical, signif digits=6

IS identical

Function ResizePadded(clip clp,Int W, Int H,Int "xMod",Int "yMod",Float "Bicub_b",Float "Bicub_c") {
# https://forum.doom9.org/showthread.php?t=174496
# Intended for downsize, where return clip dimensions are modulo xMod,yMod but some of result frame (right and bottom) may be padding,
# ie if resize to W=29,H=29, and xMod=4,yMod=4, then frame will be 32x32, x=0,y=0,w=29,h=29 is valid graphic,
# but rightmost and bottom most 3 pixels are padding only. For use where mask used to apply valid graphic using eg Overlay.
xMod=Default(xMod,2) yMod=Default(yMod,2)
# DEFAULT:- BicubicResize b and c. (If given as args, BEST SUPPLY BOTH Bicub_b and Bicub_c)
# DownSize b = -0.5, c = 0.25, Groucho2004,Didée:- https://forum.doom9.org/showthread.php?p=1802716#post1802716
# Upsize, b = 0.0, c = 0.5, Catmull-Rom spline, sharp.
# Avisynth defaults are:- b=1.0/3, c=1.0/3
Bicub_b = Default(Bicub_b, (W<clp.Width) ? -0.5 : 0.0)
Bicub_c = Default(Bicub_c, (W<clp.Width) ? 0.25 : 0.5)
CanvasW=(W+xMod-1)/xMod*xMod CanvasH=(H+yMod-1)/yMod*yMod
dAddX=CanvasW-W dAddY=CanvasH-H
PadX=Float(dAddX)*clp.Width/W PadY=Float(dAddY)*clp.Height/H
clp.BicubicResize(CanvasW, CanvasH, b=Bicub_b, c=Bicub_c , src_left=0, src_top=0, src_width=clp.width+PadX, src_height=clp.height+PadY)
Return Last
}

AvisourcE("F:\V\XMEN2.avi")
O=Last

A=O.ResizePadded(640,480,Bicub_b=1.0/3,Bicub_c=1.0/3)
B=O.ResizePadded(640,480,Bicub_b=0.333333,Bicub_c=0.333334) # IS IDENTICAL

PixelsDifferent=0
SSS="""
PixelsDifferent=PixelsDifferent + RT_LumaPixelsDifferentCount(Last,B)
RT_Subtitle("%d",PixelsDifferent)
"""

Return A.Scriptclip(SSS) # Shows 0, after several minutes

age
6th April 2017, 18:42
Well it's good to know.
So in avisynth bicubic with b=0.333333 c=0.333334 is identical to Mitchell b=c=1/3 :cool:

BakaProxy
7th April 2017, 06:23
Spline36 has been my goto resize kernel since forever. It's reliable and probably the most "ok" looking resizer, whatever you throw at it. It may not always give the best results possible but fine tuning isn't always an option or even preferred (because it's hella time consuming).

Verstuurd vanaf mijn SM-A500FU met Tapatalk

FranceBB
8th April 2017, 06:17
Mine is Spline64Resize via NNEDI. I'm satisfied with results every time, especially if I'm downscaling at 16bit. If I gotta bring an upscaled material to its original resolution, instead, I use debilinear or debicubic at 16bit to reverse upscale.

BakaProxy
8th April 2017, 14:07
Mine is Spline64Resize via NNEDI. I'm satisfied with results every time, especially if I'm downscaling at 16bit. If I gotta bring an upscaled material to its original resolution, instead, I use debilinear or debicubic at 16bit to reverse upscale.
I hope you realize that nnedi doesn't downscale (and isn't even going to get triggered if you use tha wrapper functions) and if you're using nnedi3_rpow you're basically just upscaling the video before downscaling it. Which is considerably a waste of cpu cycles and most likely even useless (unless you're dealing with severe aliasing)

zub35
9th April 2017, 18:38
MSU Blurring metric (VQMT 9.0)

Source 3834x2154 (http://s020.radikal.ru/i706/1704/21/27764da58e29.png) - 18,046
Downscale in 1920x1080

Bicubic (http://101.imagebam.com/download/niGqLlZtiHx0GmtkjbOkCQ/54262/542614984/bicubic.png) - 13,015
Spline64 (http://101.imagebam.com/download/PTxvgvWQdAIoUoQxYxyO7A/54262/542615334/spline64.png) - 14,899
Lanczos (http://101.imagebam.com/download/6Co8XcO4lfrxJ97hbuAsUg/54262/542615146/lanczos.png) - 15,149
ResizeShader-SSim (http://101.imagebam.com/download/lGJA5DoHJgre_GRFJyCWBw/54262/542615379/SSim.png) - 18,105

Katie Boundary
10th April 2017, 06:33
Hi guys,

I'm wondering if there is any consensus on a better resizer for downsizing ?

This pretty much seems to work ok, but would like recommendation for the actual downsizer (I guess might be used for upsize too).

Is there any particular reason why "pixel mixing" or "area averaging" wasn't an option?

theoretically mitchell bicubic follows the "b+2c=1" formula, b=c=1/3 ==> (1/3) + 2*(1/3)=1 but in computer programming 1/3=0.3333333 is an approximation and b+2c=0.9999999
so we can force b+2c=1 with truncated b/c parameters:
b =0.33333334 c=1/3=0.33333333 ==> b+2c=1

Seeing as how there's nothing unique and magical about B=C, you might as well just save a few keystrokes and plug in b=0.34, c=0.33, or even b=0.4, c=0.3

StainlessS
10th April 2017, 11:14
Is there any particular reason why "pixel mixing" or "area averaging" wasn't an option?


No. :)

hello_hello
13th April 2017, 02:59
I'm probably going to feel silly but I don't understand this line in the script.

CanvasW=(W+xMod-1)/xMod*xMod

Isn't that the same as this?

CanvasW=W+xMod-1

feisty2
13th April 2017, 03:04
x/y for integers does floor(x/y) implicitly

hello_hello
13th April 2017, 03:22
Ahhhhh.... the penny has dropped. Ta.

wonkey_monkey
14th April 2017, 00:31
Seeing as how there's nothing unique and magical about B=C

I feel unwilling to take your word for this, for some reason.

Anyone?

MysteryX
14th April 2017, 00:38
MSU Blurring metric (VQMT 9.0)

Source 3834x2154 (http://s020.radikal.ru/i706/1704/21/27764da58e29.png) - 18,046
Downscale in 1920x1080

Bicubic (http://101.imagebam.com/download/niGqLlZtiHx0GmtkjbOkCQ/54262/542614984/bicubic.png) - 13,015
Spline64 (http://101.imagebam.com/download/PTxvgvWQdAIoUoQxYxyO7A/54262/542615334/spline64.png) - 14,899
Lanczos (http://101.imagebam.com/download/6Co8XcO4lfrxJ97hbuAsUg/54262/542615146/lanczos.png) - 15,149
ResizeShader-SSim (http://101.imagebam.com/download/lGJA5DoHJgre_GRFJyCWBw/54262/542615379/SSim.png) - 18,105

Personally I like the result of SSim very much -- but it does amplify mosquito noise. I'd suggest that in such case, mosquito noise should be removed first anyway.

Does anyone see other downsides to it? (besides performance)

Also, what Sharpness/Soft settings did you use for SSim? Perhaps using the Soft variant would reduce the mosquito amplification. I believe the ideal downsizer should have the same blurring metrics as the source. With your settings, SSim is just slightly too sharp. Also it would be good to compare Bicubic with suggested B and C settings.

StainlessS
14th April 2017, 11:39
Ahhhhh.... the penny has dropped. Ta.

HH, As Feisty said,
but, the effect of the entire line is to round UP to the next higher multiple of XMod (if not an exact multiple of XMod already).

hello_hello
15th April 2017, 09:45
StainlessS,
I gave myself dummy of the week award for not realising, especially as I just finished revamping my own cropping and resizing script (https://forum.videohelp.com/threads/382888-CropResizeBorder-script) which also included similar adjustments to achieve a particular mod. :)

StainlessS
15th April 2017, 10:11
I gave myself dummy of the week award

HeHeHeHeHe https://www.cosgan.de/images/midi/froehlich/d050.gif

My nearly all time favorite of my posts, is in response to this:- https://forum.doom9.org/showthread.php?p=1729220#post1729220

See the following few posts.

StainlessS
16th April 2017, 22:36
I have modified OP a little, posting here first before sister thread:- https://forum.doom9.org/showthread.php?t=174527
simply because it was the "Headline" post here.

Mods,
Added args

Int "src_left", Int "src_top", Int "src_width", Int "src_height",
\ Bool "Pad_Rec601"


Selectable coords of input clip, [as per Crop()]
and output padding color for YUV Black, either 0 (default or 16 rec601).
Changed padding output to black.

Perhaps some scripters/scriptors/scriptess's could check it out, I'm not 100% certain that I've done it correctly. (thanks).

EDIT: xMod, yMod now default to the source clip natural colorspace alignment requirement rather than 2.
Now requires both RT_Stats and mt_tools v2.0.

EDIT Added check on xMod, yMod

Assert(0 < xMod && xMod % DefXmod == 0, RT_String("%sIllegal xMod(%d, DefXMod=%d)",myName,xMod,DefXmod))
Assert(0 < yMod && yMod % DefYmod == 0, RT_String("%sIllegal yMod(%d, DefYMod=%d)",myName,yMod,DefYMod))

EDIT: Added update to sister thread without thread bump.

EDIT: Oops, fixed cockup. Added in BLUE. EDIT: As we are resizing, it will not be Interlaced, will it !


DefXmod = RT_ColorSpaceXMod DefYmod = RT_ColorSpaceYMod(Laced=False) # Natural ColorSpace alignment for input clip

Gser
5th August 2017, 21:13
Personally I like the result of SSim very much -- but it does amplify mosquito noise. I'd suggest that in such case, mosquito noise should be removed first anyway.

Does anyone see other downsides to it? (besides performance)

Also, what Sharpness/Soft settings did you use for SSim? Perhaps using the Soft variant would reduce the mosquito amplification. I believe the ideal downsizer should have the same blurring metrics as the source. With your settings, SSim is just slightly too sharp. Also it would be good to compare Bicubic with suggested B and C settings.

MadVR has an anti-bloating feature that work wonderfully with SSim downscaling. I wonder if it is possible it add it to AvisynthShader. PS. Thanks a lot for your work, literally the only implementation I've seen of SSim for avisynth.