Log in

View Full Version : [Solved] Check if porting is proper


Overdrive80
27th September 2015, 13:41
Hi, folks. I am porting some functions for beginning to use vapoursynth.

I need convert this function:

function BrightDfttestMod(clip input, float "sbright", float "smedium", float "sdark",
\ int "th_low", int "th_med", int "th_high", int "tbsize", bool "lsb", int "mode")
{


sbright = default( sbright, 0 )
smedium = default( smedium, 0 )
sdark = default( sdark, 0 )

th_low = default( th_low, 20 )
th_med = default( th_med, 40 )
th_high = default( th_high, 100 )

lsb = default( lsb, false )
mode = default( mode, 0 )


bright = (sbright > 0 && lsb==false) ? input.dfttest(sigma = sbright,tbsize=tbsize,sbsize=18,sosize=9,lsb=lsb):\
(sbright > 0 && lsb==true) ? input.dfttest(sigma = sbright,tbsize=tbsize,sbsize=18,sosize=9,lsb=lsb).ditherpost(mode=mode) : input
medium = (smedium > 0 && lsb==false) ? input.dfttest(sigma = smedium,tbsize=tbsize,sbsize=18,sosize=9,lsb=lsb):\
(smedium > 0 && lsb==true) ? input.dfttest(sigma = smedium,tbsize=tbsize,sbsize=18,sosize=9,lsb=lsb).ditherpost(mode=mode) : input
dark = (sdark > 0 && lsb==false) ? input.dfttest(sigma = sdark,tbsize=tbsize,sbsize=18,sosize=9,lsb=lsb) :\
(sdark > 0 && lsb==true) ? input.dfttest(sigma = sdark,tbsize=tbsize,sbsize=18,sosize=9,lsb=lsb).ditherpost(mode=mode) : input

mmask = medium.levels(th_med, 1.0, th_high, 255, 0, false)
dmask = medium.levels(th_low, 1.0, th_med, 255, 0, false)

output = bright.mt_merge(medium, mmask, U=3, V=3, luma=true)
\ .mt_merge(dark, dmask, U=3, V=3, luma=true)

return output
}

Other question is how could test this function, where put this file.py??

Thanks

YamashitaRen
27th September 2015, 14:39
You have to put your function in the python path (I don't know where it is on your system).

For testing it, let's assume you named the file "myfunction.py" :

import vapoursynth as vs
import myfunction as mf
core=vs.get_core()

clip=mysourceclip
clip=BrightDfttestMod(clip)
clip.set_output()


Or, instead of using the "import myfunction as mf", you can copy/paste your function in the script. Which is good for testing with vsedit in my opinion.

ps : you forgot the import "vapoursynth as vs" before "core = vs.get_core()"

Are_
27th September 2015, 14:42
I don't have the time to check it now but:

- You forgot to load vapoursynth in this module:
import vapoursynth as vs
And in the very first line, outside of any function.

- Don't manually load any plug-in, anywhere, never (well, just for debugging). It is needed in avisynth because it's broken there, but auto-loading is quite sane in vapoursynth.

- Vapoursynth is case sensitive, so, you will be happier if you don't use caps in function names/parameters, just write all lowercase or use underscores.

- Your indentation is not consistent, it will error out.

- """ are used for documentation, use # for general comments.

And to test functions is best to put it in a working script with some source file loaded, and put them in a separate file in "/your/python/installation/site-packages", if you don't you will need to restart the testing program to reload the module, quite annoying.

Overdrive80
27th September 2015, 14:54
Thanks, a both.

Your indentation is not consistent, it will error out.

Exactly. Edit: Fixed.

I put module py in "C:\Users\Isra\AppData\Local\Programs\Python\Python35-32\Lib\site-packages". From Vapoursynth editor I use:

import vapoursynth as vs
import brightdfttestmod
core = vs.get_core()

clip = core.lsmas.LWLibavSource(r'D:/Utorrent/xxx.mp4')

clip=brightdfttestmod(clip)
clip.set_output()

With updated above code this is error message:


File "src\cython\vapoursynth.pyx", line 1484, in vapoursynth.vpy_evaluateScript (src\cython\vapoursynth.c:26808)
File "C:/Users/Isra/Desktop/Untitled.vpy", line 7, in <module>
clip=brightdfttestmod(clip)
TypeError: 'module' object is not callable

videoh
27th September 2015, 15:48
Just a noob here but maybe something like this:

import vapoursynth as vs
import brightdfttestmod as br
core = vs.get_core()

clip = core.lsmas.LWLibavSource(r'D:/Utorrent/xxx.mp4')

clip=br.brightdfttestmod(clip)
clip.set_output()

Are_
27th September 2015, 20:44
After reading your function and realizing you didn't even boder to read the documentation for any plugin (Levels had transposed arguments and many functions had nonexistent arguments/function_names) I lost part of my motivation to help you. :(

Anyway here is like it may be done:
import vapoursynth as vs

def scale(old_value, new_bd=16):
return int((old_value * ((1 << new_bd) - 1)) / 255)


def brightdfttestmod(clip, sbright=0, smedium=0, sdark=0, th_low=20, th_med=40, th_high=100, tbsize=5):

core = vs.get_core()

if not isinstance(clip, vs.VideoNode):
raise ValueError('This is not a clip')

# This scales the values for Levels in case we are not working with 8-bit clips
if clip.format.bits_per_sample != 8:
th_low = scale(th_low, clip.format.bits_per_sample)
th_med = scale(th_med, clip.format.bits_per_sample)
th_high = scale(th_high, clip.format.bits_per_sample)

# Bright
if sbright > 0:
bright = core.dfttest.DFTTest(clip, sigma=sbright, tbsize=tbsize, sbsize=18, sosize=9)
else:
bright = clip

# Medium
if smedium > 0:
medium = core.dfttest.DFTTest(clip, sigma=smedium, tbsize=tbsize, sbsize=18, sosize=9)
else:
medium = clip

# Dark
if sdark > 0:
dark = core.dfttest.DFTTest(clip, sigma=sdark, tbsize=tbsize, sbsize=18, sosize=9)
else:
dark = clip

mmask = core.std.Levels(medium, th_med, th_high, 1.0, (1 << clip.format.bits_per_sample) - 1, 0)
dmask = core.std.Levels(medium, th_low, th_med, 1.0, (1 << clip.format.bits_per_sample) - 1, 0)

output = core.std.MaskedMerge(bright, medium, mmask, first_plane=1)
output = core.std.MaskedMerge(output, dark, dmask, first_plane=1)

return output


Carefully read it and try to notice what you were doing wrong.
I also get ride of lsb because it makes no sense when all your entire pipeline can be at any bit depth.

Overdrive80
27th September 2015, 20:51
Just a noob here but maybe something like this:

import vapoursynth as vs
import brightdfttestmod as br
core = vs.get_core()

clip = core.lsmas.LWLibavSource(r'D:/Utorrent/xxx.mp4')

clip=br.brightdfttestmod(clip)
clip.set_output()

Some answer is appreciatted. Thanks, was useful.

Edit: Finally, I get that works. Now, I have compare result of avisynth and vapoursynth

Overdrive80
27th September 2015, 21:09
After reading your function and realizing you didn't even boder to read the documentation for any plugin (Levels had transposed arguments and many functions had nonexistent arguments/function_names) I lost part of my motivation to help you. :(

Is my first attempt with vapoursynth, I read documentation but I can not convert on advanced user at one day. Sorry that you think it and you lost motivation, but if you see modification of code, you can check that I do changes about it.

ATW, thanks for answering.

Overdrive80
28th September 2015, 09:29
Hi holywu, use: ( U=3, V=3, luma=true) isnt the same than planes=[1, 2], first_plane=True , is it??

With respect to lsb and mode, until he said it I did not realize that vapoursynth processed over 8 bitdepth unlike avisynth, hence my misconception.

Thanks to his code, I call to function after using: clip=core.fmtc.bitdepth(clip, bits=16, mode=6)

Thanks for helping to learn.

Are_
28th September 2015, 12:02
Hi holywu, use: ( U=3, V=3, luma=true) isnt the same than planes=[1, 2], first_plane=True , is it??
No.
U=3, V=3, luma=true means: process both chroma planes (in adition to luma plane), use luma plane from mask to do it.
planes=[1, 2], first_plane=True means: proces only chroma planes, use luma plane from mask to do it.

As I posted in my script, you should just put first_plane=True, because planes=[0, 1, 2] is the default, or if you want to put everything there, planes=[0, 1, 2], first_plane=True

Overdrive80
28th September 2015, 14:08
No.
U=3, V=3, luma=true means: process both chroma planes (in adition to luma plane), use luma plane from mask to do it.
planes=[1, 2], first_plane=True means: proces only chroma planes, use luma plane from mask to do it.

As I posted in my script, you should just put first_plane=True, because planes=[0, 1, 2] is the default, or if you want to put everything there, planes=[0, 1, 2], first_plane=True

Ok, all clear. Thanks you so much.

Overdrive80
1st October 2015, 19:31
For how want, I have completed the function (thanks to _Are):

import vapoursynth as vs

def scale(old_value, new_bd=16):
return int((old_value * ((1 << new_bd) - 1)) / 255)


def brightdfttestmod(clip, sbright=0, smedium=0, sdark=0, th_low=20, th_med=40, th_high=100, tbsize=5,
sbsize=18, sosize=9, planes=[0,1,2], luma=True):

core = vs.get_core()

if not isinstance(clip, vs.VideoNode):
raise ValueError('This is not a clip')

# This scales the values for Levels in case we are not working with 8-bit clips
if clip.format.bits_per_sample != 8:
th_low = scale(th_low, clip.format.bits_per_sample)
th_med = scale(th_med, clip.format.bits_per_sample)
th_high = scale(th_high, clip.format.bits_per_sample)

# Bright
if sbright > 0:
bright = core.dfttest.DFTTest(clip, sigma=sbright, tbsize=tbsize, sbsize=sbsize, sosize=sosize, planes=planes)
else:
bright = clip

# Medium
if smedium > 0:
medium = core.dfttest.DFTTest(clip, sigma=smedium, tbsize=tbsize, sbsize=sbsize, sosize=sosize, planes=planes)
else:
medium = clip

# Dark
if sdark > 0:
dark = core.dfttest.DFTTest(clip, sigma=sdark, tbsize=tbsize, sbsize=sbsize, sosize=sosize, planes=planes)
else:
dark = clip

if luma==True:
luma_layer= core.std.ShufflePlanes(medium, planes=[0], colorfamily=vs.GRAY)
mmask = core.std.Levels(luma_layer, th_med, th_high, 1.0, (1 << clip.format.bits_per_sample) - 1, 0)
dmask = core.std.Levels(luma_layer, th_low, th_med, 1.0, (1 << clip.format.bits_per_sample) - 1, 0)
else:
mmask = core.std.Levels(medium, th_med, th_high, 1.0, (1 << clip.format.bits_per_sample) - 1, 0)
dmask = core.std.Levels(medium, th_low, th_med, 1.0, (1 << clip.format.bits_per_sample) - 1, 0)

output = core.std.MaskedMerge(bright, medium, mmask, planes=planes, first_plane=luma)
output = core.std.MaskedMerge(output, dark, dmask, planes=planes, first_plane=luma)

return output

Myrsloik
1st October 2015, 20:34
Why oh WHY are you putting so many lines into making your functions not work the same way as basically every other vapoursynth function ever?

And a small hint if you still want to make things more difficult:
planes = []
if y:
planes.insert(0)
...
if planes.count() == 0:
error terror!

Are_
1st October 2015, 20:43
No really, just forget about all that avisynth nonsense, instead of y=True, u=True, v=True just do planes=[0, 1, 2], that will make your function look like any vapoursynth function out there :). And pass that to dfttest too, if you are not going to use it don't spend time filtering it.
And before you create the mask you can just core.std.ShufflePlanes(medium, planes=[0], colorfamily=vs.GRAY), so you can get ride of first_plane=luma too.

Overdrive80
1st October 2015, 22:21
Why oh WHY are you putting so many lines into making your functions not work the same way as basically every other vapoursynth function ever?

And a small hint if you still want to make things more difficult:
planes = []
if y:
planes.insert(0)
...
if planes.count() == 0:
error terror!

Hi, are you referring to some like this, arent you?:

planes=[]
# Select planes
if y:
planes.append(0)
if u:
planes.append(1)
if v:
planes.append(2)
if len(planes)==0:
raise ValueError("Really? There are not any plane selected.")

Myrsloik
1st October 2015, 22:43
Yes, at least write reasonable code to do horrible things.

Overdrive80
1st October 2015, 23:18
No really, just forget about all that avisynth nonsense, instead of y=True, u=True, v=True just do planes=[0, 1, 2], that will make your function look like any vapoursynth function out there :).

Fixed, like Myrsloik suggestion, too.

And pass that to dfttest too, if you are not going to use it don't spend time filtering it.

Fixed.

And before you create the mask you can just core.std.ShufflePlanes(medium, planes=[0], colorfamily=vs.GRAY), so you can get ride of first_plane=luma too.

If I dont wrong, do you say this?:

luma_layer= core.std.ShufflePlanes(medium, planes=[0], colorfamily=vs.GRAY)
mmask = core.std.Levels(luma_layer, th_med, th_high, 1.0, (1 << clip.format.bits_per_sample) - 1, 0)
dmask = core.std.Levels(luma_layer, th_low, th_med, 1.0, (1 << clip.format.bits_per_sample) - 1, 0)

Are_
1st October 2015, 23:53
Yes, like that.

Overdrive80
2nd October 2015, 22:30
Updated function.