Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Usage

Reply
 
Thread Tools Search this Thread Display Modes
Old 4th April 2017, 08:18   #1  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
Recomendation for Downsizer required - SOLVED.

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).

Code:
# 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 )
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 16th April 2017 at 23:52. Reason: Oops, broke it, fixed.
StainlessS is offline   Reply With Quote
Old 4th April 2017, 08:33   #2  |  Link
Groucho2004
 
Join Date: Mar 2006
Location: Barcelona
Posts: 5,034
You're going to get a bunch of opinions, everyone has their own personal favourite. Here's mine:
Code:
BicubicResize(w, h, b = -0.5, c = 0.25)
This was inspired by Didée, see this post.

Also, see this thread
__________________
Groucho's Avisynth Stuff

Last edited by Groucho2004; 4th April 2017 at 08:35.
Groucho2004 is offline   Reply With Quote
Old 4th April 2017, 08:43   #3  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
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.
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 4th April 2017 at 11:14.
StainlessS is offline   Reply With Quote
Old 4th April 2017, 17:05   #4  |  Link
Gser
Registered User
 
Join Date: Apr 2008
Posts: 418
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.

Last edited by Gser; 4th April 2017 at 17:09.
Gser is offline   Reply With Quote
Old 4th April 2017, 20:31   #5  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
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.
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 6th April 2017 at 22:36.
StainlessS is offline   Reply With Quote
Old 5th April 2017, 14:13   #6  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
Added BicubicResize() optional Bicub_b and Bicub_c args to first post script, probably no further alterations.

Best supply both or none.
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???
StainlessS is offline   Reply With Quote
Old 6th April 2017, 13:35   #7  |  Link
age
Registered User
 
Join Date: Oct 2015
Posts: 54
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
age is offline   Reply With Quote
Old 6th April 2017, 14:40   #8  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
Code:
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
Code:
B=O.ResizePadded(640,480,Bicub_b=0.333330,Bicub_c=0.333339)      # signif digits=6, Occasional difference seen, when AMP
Quote:
#B=O.ResizePadded(640,480,Bicub_b=0.333334,Bicub_c=0.333333) # Seems identical, signif digits=6
IS identical
Code:
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
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 1st August 2018 at 07:00.
StainlessS is offline   Reply With Quote
Old 6th April 2017, 18:42   #9  |  Link
age
Registered User
 
Join Date: Oct 2015
Posts: 54
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
age is offline   Reply With Quote
Old 7th April 2017, 06:23   #10  |  Link
BakaProxy
Registered User
 
Join Date: Jan 2015
Posts: 47
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
BakaProxy is offline   Reply With Quote
Old 8th April 2017, 06:17   #11  |  Link
FranceBB
Broadcast Encoder
 
FranceBB's Avatar
 
Join Date: Nov 2013
Location: Royal Borough of Kensington & Chelsea, UK
Posts: 2,883
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.
FranceBB is offline   Reply With Quote
Old 8th April 2017, 14:07   #12  |  Link
BakaProxy
Registered User
 
Join Date: Jan 2015
Posts: 47
Quote:
Originally Posted by FranceBB View Post
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)
BakaProxy is offline   Reply With Quote
Old 9th April 2017, 18:38   #13  |  Link
zub35
Registered User
 
Join Date: Oct 2016
Posts: 56
MSU Blurring metric (VQMT 9.0)

Source 3834x2154 - 18,046
Downscale in 1920x1080

Bicubic - 13,015
Spline64 - 14,899
Lanczos - 15,149
ResizeShader-SSim - 18,105

Last edited by zub35; 9th April 2017 at 18:42.
zub35 is offline   Reply With Quote
Old 10th April 2017, 06:33   #14  |  Link
Katie Boundary
Registered User
 
Katie Boundary's Avatar
 
Join Date: Jan 2015
Posts: 1,048
Quote:
Originally Posted by StainlessS View Post
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?

Quote:
Originally Posted by age View Post
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
__________________
I ask unusual questions but always give proper thanks to those who give correct and useful answers.
Katie Boundary is offline   Reply With Quote
Old 10th April 2017, 11:14   #15  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
Quote:
Originally Posted by Katie Boundary View Post
Is there any particular reason why "pixel mixing" or "area averaging" wasn't an option?
No.
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???
StainlessS is offline   Reply With Quote
Old 13th April 2017, 02:59   #16  |  Link
hello_hello
Registered User
 
Join Date: Mar 2011
Posts: 4,823
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
hello_hello is offline   Reply With Quote
Old 13th April 2017, 03:04   #17  |  Link
feisty2
I'm Siri
 
feisty2's Avatar
 
Join Date: Oct 2012
Location: void
Posts: 2,633
x/y for integers does floor(x/y) implicitly
feisty2 is offline   Reply With Quote
Old 13th April 2017, 03:22   #18  |  Link
hello_hello
Registered User
 
Join Date: Mar 2011
Posts: 4,823
Ahhhhh.... the penny has dropped. Ta.
hello_hello is offline   Reply With Quote
Old 14th April 2017, 00:31   #19  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
Join Date: Jan 2004
Posts: 2,493
Quote:
Originally Posted by Katie Boundary View Post
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?
__________________
My AviSynth filters / I'm the Doctor
wonkey_monkey is offline   Reply With Quote
Old 14th April 2017, 00:38   #20  |  Link
MysteryX
Soul Architect
 
MysteryX's Avatar
 
Join Date: Apr 2014
Posts: 2,559
Quote:
Originally Posted by zub35 View Post
MSU Blurring metric (VQMT 9.0)

Source 3834x2154 - 18,046
Downscale in 1920x1080

Bicubic - 13,015
Spline64 - 14,899
Lanczos - 15,149
ResizeShader-SSim - 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.

Last edited by MysteryX; 14th April 2017 at 00:42.
MysteryX is offline   Reply With Quote
Reply

Tags
downsize, padding, resize

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:37.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.