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 Development

Reply
 
Thread Tools Search this Thread Display Modes
Old 7th July 2020, 14:28   #1  |  Link
DTL
Registered User
 
Join Date: Jul 2018
Posts: 10
Sin squared kernel for resize (Gaussresize mod)

As numerical modeling shows sinus square function gives a bit narrower single pulse with given comparable outside non-linearity and non-monotonicality (ringing) in compare with gaussian function.

The modeling was done with web-accessible setup - you can check it at http://drakan.ru/FTProot/vid_work/LR...g_rest_en.html

For example at sigma = 0.85 gauss pulse has 0.5-level duration about 2T


and with T=3.7 sin squared pulse has 0.5-level duration 1.9T and significally better form near lower values.


Sinus squared function (pulse/kernel) is f(arg,T) (T as parameter)

for(abs(arg) < PI/2)
{
(cos(arg*T*PI))^2
}
else
0

(To be centered around 0 we replace sin(arg+pi/2) to cos() )/

Currently in avisynth only GaussResize gives acceptable results in attempts to get good anti-alized fonts for example or doing other high-quality anti-alized work. May be because of better pre-filtering.

So I assume using of sinus squared kernel instead of gaussian may gives a bit sharper results with same level of artifacts (ringing) control.

As building todays avisynth (+) is enough difficult for low-skilled programmer it will be nice if anyone with existing anisynth-build enviroment will make an additional resize function almost completely based on current GaussResize function with changed gauss kernel to sinus-squared kernel.
Also with additional parameter similair as GaussianResize have.

Last edited by DTL; 8th July 2020 at 12:47.
DTL is offline   Reply With Quote
Old 7th July 2020, 14:34   #2  |  Link
DTL
Registered User
 
Join Date: Jul 2018
Posts: 10
For example of using GaussResize for getting antialiased video-font (compared with curent simple built-in avisynth font raserizer) the script can be used:
Code:
function Ast(clip c, int isize)
{
 return Subtitle(c, "Font",font="Arial",size=isize,align=6,halo_color=$FF000000, text_color=$00e0e0e0)
}

function Ast2(clip c, int isize)
{
 return Subtitle(c, "Font",font="Arial",size=isize,x=10,y=20,halo_color=$FF000000, text_color=$00e0e0e0)
}

BlankClip(100,480,290,"RGB24",25)

Animate(last, 0,100,"Ast2",  80,  180)

AddBorders(100,100,600,200)
Levels(0, 1, 255, 10, 235)

GaussResize(width/8,height/8,p=15)

Animate(0,100,"Ast",  11,  22)

SincResize(last,width*10,height*10,taps=20) (last resize must be omitted to get 100% size output, it only simulates video control monitor processing)
Looking in the sources of current avisynth+ release (3.6.1) it may looks like (derived from Sinc and Gauss resize existing filters):

Addition to resample_functions.cpp:

Code:
/***********************
 *** SinSquared filter ***
 ***********************/

SinSquaredFilter::SinSquaredFilter(double p) {
  param = clamp(p, 0.1, 100.0);
}

double SinSquaredFilter::f(double value) {
   value = fabs(value);
   double p = param*0.1;
   value *= M_PI*p;

 if (value < M_PI/2) 
{
    return cos(value)*cos(value);
}
else
 return 0;
}

and to resample_functions.h :

class SinSquaredFilter : public ResamplingFunction
{
public:
  SinSquaredFilter(double p = 30.0);
	double f(double x);
	double support() { return 4.0; };

private:
 double param;
};
and may be to other places to 'register' new filter. At first tests may be one can just rewrite existing GaussResize function.

The changes are small enough but to test them it is required to build locally full avisynth build enviroment. Or may be any online way for building avisynth available to get working binary for test ? Ofcourse debugging ability will be lost in compare with full building and debugging local enviroment.

Last edited by DTL; Yesterday at 16:44. Reason: fixed code changes a bit
DTL is offline   Reply With Quote
Old 8th July 2020, 08:50   #3  |  Link
jpsdr
Registered User
 
Join Date: Oct 2002
Location: France
Posts: 1,823
Oh, interesting, i think i can easely add this to my plugin. PM an email i can send you a build test.
__________________
My github.

Last edited by jpsdr; 8th July 2020 at 08:53.
jpsdr is offline   Reply With Quote
Old 9th July 2020, 20:10   #4  |  Link
DTL
Registered User
 
Join Date: Jul 2018
Posts: 10
One more promising kernel around powered-sinus/cosinus:

(for double *Filter::f(double value) function with all the same other lines )
Code:
if (value < M_PI/2)
{
return pow(cos(value), 1.7);
}
else
if (value < M_PI * 0.8)
return (-1)*cos(value) * cos(value)/value;
else
return 0;
While gaussian and limited to +-pi/2 cosine-squared kernels gives both ringing-controlled and halo/peaking(over/undershooting) controlled shape it gives significally lower sharpness. We can try to make kernel with halo/peaking control but with reduced ringing (outside our impulse borders) as possible. So it is my attempt to combine cosine-squared (real ^1.7 looks works better) impulse with inverted cosine-squared (unfortunately pow() functions looks like illegal of powering to 2.0- of negative numbers, but we can possibly replace negative cos(pi/2+) to positive sin/cos shape shifted to pi..pi/2 in the future versions) impulse dividev to argument for better decay.
In the range of T parameter 2.6 to 3.6 it gives sharpened shape with controlled undershoots amplitude and duration with enough control ringing outside undershoots. The lowest possible 0.5-level duration is about 1.4T with T-parameter 2.6. It is significally closer to theoretical 1.2T limit of 0.5-level of sinc base impulse in compare with gaussian and sin/cos suqared kernels.

May this can be called SinPowResize to separate with SinSqResize filter.

Though due to significant negative values this kernel definetly require float processing engine or at least signed integer instead of unsigned integer. I hope in todays avisynth resample in ASM is designed for handling negative coefficients as well as positive.

Updated web-simulator - http://drakan.ru/FTProot/vid_work/LR...g_rest_en.html it has Sin^p button now.



Unfortunately using limits like "value < M_PI * 0.8" looks like dirty hacking that works with simple 5-point simulator and some limited range or T-parameter but may cause bugs with real resample engine because I do not know which values of 'value' parameter actually passes to f(value) kernel function. Still do not have working avisynth-building enviroment for testing.

Last edited by DTL; Yesterday at 16:43.
DTL is offline   Reply With Quote
Old 10th July 2020, 10:03   #5  |  Link
DTL
Registered User
 
Join Date: Jul 2018
Posts: 10
Quick tests of the over/under shooted kernel in real processing in avisynth (built-in sincresize to size*4):

T=2.7, downscaled 3 times to fit 0..255 range (without negative values illegal to integer 8-bit coding of typical video/images). Samples put by hand in ms-paint into image array. (16),1,31,89,31,1,(16) (in the right side of image, at the left is sort of sin^2 or gauss pulse without over/undershoots (16),46,89,46,(16). 800% screenshot of ms-paint.

Later loaded in avisynth with imagereader, scaled with sincresize to size*4 with max available taps=20 and displayed in virtualdub as 50% and 100% size. The undershooted pulse shows very good conditioned against ringing with visually sharper view. Ofcourse vertically it is unconditioned against any Nyquist limits becaue it is 1-dimentional test, so it do shows ringing vertically.
Attachments Pending Approval
File Type: png o_u_k27_res01.png
File Type: png inp_800p_01.png
File Type: png sin_p_t27_01.png

Last edited by DTL; 10th July 2020 at 10:21.
DTL is offline   Reply With Quote
Old 11th July 2020, 10:51   #6  |  Link
DTL
Registered User
 
Join Date: Jul 2018
Posts: 10
Thanks to jpsdr for providing test builds. He makes permission to share results and executable. It is in attach.

Current report on results:

Test script for testing 64x SSAA text with float size (or at least non-integer size) also compared to built-in Subtitle() text engine.
Code:
Loadplugin("ResampleMT.dll")

function Ast(clip c, int isize)
{
 return Subtitle(c, "Font",font="Arial",size=isize,align=6,halo_color=$FF000000, text_color=$00e0e0e0)
}

function Ast2(clip c, int isize)
{
 return Subtitle(c, "Font",font="Arial",size=isize,x=10,y=20,halo_color=$FF000000, text_color=$00e0e0e0)
}

BlankClip(100,480,150,"RGB24",25)

Animate(last, 0,100,"Ast2",  80,  180)

AddBorders(100,100,600,200)
Levels(0, 1, 255, 10, 235)

a=GaussResize(last,width/8,height/8,p=17)
b=SinSqrResizeMT(last,width/8,height/8,p=3.1)
c=SinPowResizeMT(last,width/8,height/8,p=3.4)

a=Animate(a,0,100,"Ast",  11,  22)
b=Animate(b,0,100,"Ast",  11,  22)
c=Animate(c,0,100,"Ast",  11,  22)

StackVertical(a,b,c)

SincResize(width*4,height*4,taps=20)
Levels(0, 1.2, 255, 0, 255)
2 new test functions: SinSqrResizeMT and SinPowResizeMT. Usable range of p-parameter around 2..5 float.

SinSqrResizeMT uses simple cos(arg)^2 kernel. It shows results very close to built-in Gauss resize. In given range of control parameter both ringing and over/undershoots are nicely conditioned. The contrast and sharpen of result is average.

SinPowResizeMT uses more complex kernel of cos(arg)^1.7 at +-pi/2 agrument range and -cos(arg)^2/(arg) in +-pi/2 to +-0.8pi argument range. The results are significally different: It still very good conditioned against ringing but gives more sharp results in both higher contrast and some under/overshooting (half-period non-ringed).

So we can define 2 different classes of kernels:
1. Both anti-ringing and anti-over/undershoots conditioned (gauss and cos^2).
2. Anti-ringing conditioned with (adjustable) over/undershoots that increases visual sharpness.

Unfortunately inside avisynth resampling engine the advanced kernel works in some different way in compare with my modeling and parameter p gives very small or not at all control for amplitude of over/undershooting. It mostly control overall filter frequency response from more blurry to extra sharp and ringy. Also it looks the hack of 0.8pi limiting is too illegal for sin/cos functions and gives discontiniuty. So it is good to adjust kernel function to +-pi limit with more fading members or parameters tuning. Also this class of resampling kernerls definetly needs more than 1 user-defined parameter for control both frequency response and 'sharpening' effect.

May someone can continue this work or may be I will someday get avisynth building enviremont for experiments.
Attachments Pending Approval
File Type: 7z ResampleMT_sinp.7z
File Type: png cmp_03_110720.png

Last edited by DTL; Yesterday at 16:43.
DTL is offline   Reply With Quote
Old Yesterday, 08:55   #7  |  Link
DTL
Registered User
 
Join Date: Jul 2018
Posts: 10
Oh - I found my error in parameters description, that cause difference between T-values in my simulator and 'param' in avisynth build.

In my simulator the T-param is converted to 'frequency' with reciprocal f=1/T. And I forgot it in function description. So correct relation between simulator T-param and current build avisynth function p-param is p=10/T. 10x because of 0.1 multiplication inside avisynth function.

Now things goes close as they should. The 'SinPow' kernel still has limited enough usable range in current approximation math (from T > 2.55 to T < 3.75 or p < 3.9 and p > 2.66) and I will try to fix it with my simulation work. The limited and mostly interested case is lowest duration side (lowest T-value) and as hand-positioned samples shows it can reach close to theoretical duration of sinc-pulse of 1.2T at 0.5-level limit. With about 35% undershoots of positive pluse amplitude. Keeping outside ringing acceptably controlled (at least for 8-bit SDR video).

Ofcourse it is illegal for 100% amplitide pulses in 16..235 8-bit video coding, but may work in scaled-down for contrast pulses. Like 0.2x scaled - (16),1,17,44,17,1,(16)
DTL is offline   Reply With Quote
Old Yesterday, 10:30   #8  |  Link
Selur
Registered User
 
Selur's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 6,066
@DTL: would be nice if you could use 'code'-tags, it would really help making your posts more readable.
__________________
Hybrid here in the forum, homepage
Notice: Since email notifications do not work here any more, it might take me quite some time to notice a reply to a thread,..
Selur is offline   Reply With Quote
Old Today, 10:32   #9  |  Link
DTL
Registered User
 
Join Date: Jul 2018
Posts: 10
Finally I was able to build ResampleMT with VS2013 and made corrections and debug to functions more quickly. The most important was limiting support() member to 2 instead of 4 as in GaussFilter because 4 gives errors in following avisynth resampler. The param processing is now follows web-simulator T-value (no reciprocacity and 0.1 multiplication because todays avisynth good accepts float params values).
Current kernel func:
Code:
double SinPowFilter::f(double value)
{
	value = fabs(value);
	value *= M_PI / param;

	if (value<(M_PI / 2)) return pow(cos(value), 1.8);
	else
	{
		if (value<(M_PI)) return -(cos(value)*cos(value)) / (0.9*value);
		else return 0;
	}
}
In testing with typical videocamera frames with p-parameter = 2.5 it gives comparable sharpness with Lanczos4Resize() but with better conditioned anti-ringing. Parameter p<2.5 gives better sharpness but may cause more artifacts, p>2.5 gives less sharpness. Intendent for using for downsampling at anti-alizing processing and lower frame size rip creation.
Name of function SinPowResizeMT(), parameters are same as GaussResizeMT except for p-param as descriped above. Default p=2.5. Usable range of p-param is about 2..5 float. Internally limited to 1.0..10.0.

Currently only x64 build available because win32 config gives some errors.

As for sin^2 simple kernel: It gives very close to GaussResize results, so may be no any value for most users.
Attachments Pending Approval
File Type: zip ResampleMT-master-src.zip
File Type: zip ResampleMT_x64_SinPowReisize.zip

Last edited by DTL; Today at 11:10. Reason: udpated sample script from p=7 to p=2.5
DTL is offline   Reply With Quote
Reply

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 13:43.


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