Welcome to Doom9's Forum, THE inplace 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. 
7th July 2020, 14:28  #1  Link 
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 nonlinearity and nonmonotonicality (ringing) in compare with gaussian function.
The modeling was done with webaccessible 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.5level duration about 2T and with T=3.7 sin squared pulse has 0.5level 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 antialized fonts for example or doing other highquality antialized work. May be because of better prefiltering. 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 lowskilled programmer it will be nice if anyone with existing anisynthbuild enviroment will make an additional resize function almost completely based on current GaussResize function with changed gauss kernel to sinussquared kernel. Also with additional parameter similair as GaussianResize have. Last edited by DTL; 8th July 2020 at 12:47. 
7th July 2020, 14:34  #2  Link 
Registered User
Join Date: Jul 2018
Posts: 10

For example of using GaussResize for getting antialiased videofont (compared with curent simple builtin 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) 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; }; 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 
9th July 2020, 20:10  #4  Link 
Registered User
Join Date: Jul 2018
Posts: 10

One more promising kernel around poweredsinus/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; 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.5level duration is about 1.4T with Tparameter 2.6. It is significally closer to theoretical 1.2T limit of 0.5level 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 websimulator  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 5point simulator and some limited range or Tparameter 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 avisynthbuilding enviroment for testing. Last edited by DTL; Yesterday at 16:43. 
10th July 2020, 10:03  #5  Link 
Registered User
Join Date: Jul 2018
Posts: 10

Quick tests of the over/under shooted kernel in real processing in avisynth (builtin sincresize to size*4):
T=2.7, downscaled 3 times to fit 0..255 range (without negative values illegal to integer 8bit coding of typical video/images). Samples put by hand in mspaint 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 mspaint. 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 1dimentional test, so it do shows ringing vertically. Last edited by DTL; 10th July 2020 at 10:21. 
11th July 2020, 10:51  #6  Link 
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 noninteger size) also compared to builtin 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) SinSqrResizeMT uses simple cos(arg)^2 kernel. It shows results very close to builtin 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 (halfperiod nonringed). So we can define 2 different classes of kernels: 1. Both antiringing and antiover/undershoots conditioned (gauss and cos^2). 2. Antiringing 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 userdefined 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. Last edited by DTL; Yesterday at 16:43. 
Yesterday, 08:55  #7  Link 
Registered User
Join Date: Jul 2018
Posts: 10

Oh  I found my error in parameters description, that cause difference between Tvalues in my simulator and 'param' in avisynth build.
In my simulator the Tparam is converted to 'frequency' with reciprocal f=1/T. And I forgot it in function description. So correct relation between simulator Tparam and current build avisynth function pparam 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 Tvalue) and as handpositioned samples shows it can reach close to theoretical duration of sincpulse of 1.2T at 0.5level limit. With about 35% undershoots of positive pluse amplitude. Keeping outside ringing acceptably controlled (at least for 8bit SDR video). Ofcourse it is illegal for 100% amplitide pulses in 16..235 8bit video coding, but may work in scaleddown for contrast pulses. Like 0.2x scaled  (16),1,17,44,17,1,(16) 
Today, 10:32  #9  Link 
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 websimulator Tvalue (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; } } Name of function SinPowResizeMT(), parameters are same as GaussResizeMT except for pparam as descriped above. Default p=2.5. Usable range of pparam 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. Last edited by DTL; Today at 11:10. Reason: udpated sample script from p=7 to p=2.5 
Thread Tools  Search this Thread 
Display Modes  

