Log in

View Full Version : script to automatically add borders to become MOD4/8?


leeperry
26th October 2010, 00:12
hi there,

there's many scripts/plugins that require MOD4/MOD8 height/width, it's rather annoying when using in ffdshow as sometimes you can't watch your movie in OAR :/

Would that be possible to make a function that'd add borders automatically to make it MOD4/MOD8? I'm sure it'd be useful to a lot of ffdshow users.

:thanks:

Didée
26th October 2010, 00:38
barebone:

function mmod(clip c, int h, int v) { c.addborders(0,0, (1-c.width)%h+h-1, (1-c.height)%v+v-1) }

leeperry
26th October 2010, 01:54
Thanks for the reply! but ideally it'd only pad with black borders to make it to the next mod4/mod8 dimension..to avoid processing useless black bars.

A script like mmod(mod4_height) that you could add in ffdshow would be really great :cool:

kemuri-_9
26th October 2010, 04:03
i'm thinking the h and v are supposed to be the mod factors for horizontal and vertical, but it looks like it's only supposed to work for mod 2, as any other power of 2 mod can produce incorrect results...

-Vit-
26th October 2010, 04:51
Adds borders to up to nearest multiple of modH & modV. Adds nothing when not required:

function mmod(clip c, int modH, int modV)
{
bh = modH - ((width-1)%modH + 1)
bv = modV - ((height-1)%modV + 1)
l = bh/2
r = bh - l
t = bv/2
b = bv - t
c.addborders(l,t,r,b)
}

leeperry
26th October 2010, 05:50
look great, thanks! but when I try mmod(4,4) I get this error msg: http://www.pixelz.fr/0/7/d/8c37694836c47f098d2bb6164eeb1.png

I've tried Avisynth 2.57 and 2.6 on XPSP3.

IanB
26th October 2010, 07:52
Try this :-function mmod(clip c, int modH, int modV)
{
bh = modH - ((c.Width()-1)%modH + 1)
bv = modV - ((c.Height()-1)%modV + 1)
l = bh/2
r = bh - l
t = bv/2
b = bv - t
c.AddBorders(l,t,r,b)
}

Didée
26th October 2010, 08:00
Vit's function needs to write c.width and c.height.
edit - as IanB wrote. Me was late.

My one-liner needed correction as done now.



When you're going to use this as preparation before filtering, rather go for resizing instead of adding black borders. (Imagine the filter does a gaussian blur, and your modulo thingy adds a black border that wasn't originally there. The artificial black border will influence the blur's result. For MVTools, it's also not good.)

function mmod1(clip c, int h, int v) { c.pointresize(c.width+(1-c.width)%h+h-1, c.height+(1-c.height)%v+v-1, 0,0, c.width+(1-c.width)%h+h-1, c.height+(1-c.height)%v+v-1) }

Also, Vit's script automatically distributes the borders 50/50 to left/right & top/border. That's generally smart, but if any deblocking filter is following, it will be fooled by that.

dansrfe
26th October 2010, 08:28
As I have so patiently requested in the AviSynth 2.6 thread, SEt's build for 2.6 MT has an addborders mod2 bug in which it simply refuses to add an odd number of rows of pixels even if the result is the minimum of mod2 post-addborders. For now the AviSynth 2.5.8 MT build by Jeremy Duncan works perfectly an is well up-to-date.

leeperry
26th October 2010, 08:54
My script gives 70fps w/ 2.57 and Jeremy/SEt's 2.58 and 77fps w/ SEt's 2.60...I've just tried the mmod(4,4) script w/ Jeremy's 2.58 and now I get this: http://www.pixelz.fr/0/c/7/faf9b9ea67b0c8f533b4a90258eaa.png

I guess what I wanna do is never gonna work...it's either resize or nothing, but then it'll be processing huge useless black bars :/

dansrfe
26th October 2010, 08:58
2.5.8

Windows XP: http://www.mediafire.com/?kghxrwtzmrx

Windows Vista/7: http://www.mediafire.com/?zy2xm2ejvzg

and use setmtmode(2) on top

leeperry
26th October 2010, 09:05
yes, that's Jeremy's 2.58 version I used. I get 27fps w/ setmtmode(2) on my LSF/GrainFactory3/SmoothLevels script. I guess I'm doing something wrong, like I should add Distributor() or so...but SetMTMode doesn't work too well in ffdshow IME, it seems asynchronous...not good for jitter. I'll try to get it to work.

dansrfe
26th October 2010, 09:17
2.6 definetely has more stability, no doubt. I would have used 2.6 without thinking twice if it didnt have the addborder issue which I need to work because I use a autoresize script that I made to resize all my videos to whatever size I want with whatever A/R I want which usually ends up being the source A/R and my default 1080p screen resolution.

leeperry
26th October 2010, 11:34
yeah, but then I'd need a resize that only works when it's not MOD4...this is not possible w/ ffdshow's resize, and MT() whines when its plugins change the resolution :/

I'll try to get SetMTMode to work w/ 2.58...but then I'll lose the 10% speed improvement of 2.60. Aren't there any newer 2.60 than SEt's from Sept.2009? a 2.60 w/ working mod4 + SetMTMode might be my golden ticket :)

Didée
26th October 2010, 12:04
What, you cannot use TGMC().MCTemporalDenoise().SluggishUltrasharp() in RealTime on 1080p? Get a faster machine, your current one is too lame! :p

leeperry
26th October 2010, 12:10
Nice troll attempt :p

but I prefer to watch 720p w/ killer filtering(LSF/GrainF3/SmoothLevels) that makes everything pop out of the screen :eek: than untouched 1080p by a long shot...besides 1080p DarkChip4 DLP's are still not affordable, unlike what TI's lies were saying years ago. I'm not gonna drop my 720p DC3 DLP for a 1080p DC2...not my fault if TI can't keep up w/ proper yield rates.

I thought ffdshow could resize to a mandatory resolution and add blackbars...eg. 1280*536 to 1280*764 adding blackbars to have the proper 2.39 AR, apparently it isn't :(

maybe I could put (ffdshow_dar_x)/(ffdshow_dar_y) to good use? http://leak.no-ip.org/AviSynth/ffdshow/docs/html/en/avisynth.htm

dansrfe
26th October 2010, 13:50
ok so apparently the issue around mod2/mod4 values in addborders is a legitimate error which is bypassed in 2.6. Basically whatever 2.5.8 does you can "script" 2.6 to do and remove the error. Even the the script should take like 2 mins, I haven't had my morning coffee (:D) so I'll post it a bit later. I have to revise my own scripts a bit too in order to get this to work the way I want it to in my autoresize script.

cheers

leeperry
26th October 2010, 14:00
oh, this is giving me headaches already :o

my other problem w/ using a MOD4-only plugin in MT(,4) is that it'll be sent 4 slices...and they might very well *not* be MOD4. The reason why 1280*536 works w/ SetMTMode(2,4) but not MT(,4) :rolleyes:

so I'd still need a script that adds borders to become MOD4, and either use SmoothLevels() built-in MT(which is slower than having all my scripts together on 4 cores using MT("""_""",4)) or use SetMTMode(2,4)...Avisynth 2.6 still being 10% faster than 2.57/2.58 :)

LSF works fine w/ MT(,4) on 1280*536 but SmoothLevels() doesn't...AFAIK both are MOD4, so I don't understand wth is going on..

I also dunno how to get SetMTMode() to work in VDUB so I could benchmark it..anyone knows what I should put instead of ffdshow_source() :confused: my source is ColorBars()

leeperry
27th October 2010, 05:41
ok so apparently the issue around mod2/mod4 values in addborders is a legitimate error which is bypassed in 2.6. Basically whatever 2.5.8 does you can "script" 2.6 to do and remove the error. Even the the script should take like 2 mins, I haven't had my morning coffee (:D) so I'll post it a bit later. I have to revise my own scripts a bit too in order to get this to work the way I want it to in my autoresize script.
ok, cool! coz I'm currently using SetMTMode(2,4) and everything works fine....too bad IanB's script is no workee w/ 2.60. That's mmod(4,4) on a 1280*585 upscale: http://img259.imageshack.us/img259/2073/40960257.png

I guess it all boils down to this post (http://forum.doom9.org/showthread.php?p=1453348#post1453348), would anyone know how to get it to work please?

IanB
27th October 2010, 08:17
Don't blame me if -Vit-'s script, that I corrected the syntax errors in, does not do what you want.

Specify exactly what you want from a script function and I may help you write one.

leeperry
27th October 2010, 08:31
Hi IanB, sorry! I'm always glad to receive your expert advise :)

If you don't mind me asking again, would you know how to benchmark SetMTMode() using ColorBars() in VDUB? It seems to need a ___source() and I dunno what to put. It's easy to benchmark MT(), but isn't it possible to benchmark SetMTMode() as well?

I currently use that script you posted a while back, it works wonders w/ MT():
SetMemoryMax(128) # Adjust value as required

ColorBars(1280, 720, pixel_type="YV12") # Size and pixel type as appropriate

# Insert Code under test here

SmoothLevels(preset="tv2pc",multithread=2,Smode=0,useopt=true,dither=50)
MT("""
LimitedSharpenFaster()
""",4)

# Throw away most of the image we are not testing VD's i/o
Crop(0, 0, 16, 16)

Well, I'd love a script that'd add borders to make it to the next MOD4 dimension automatically...eg 720*400 upscaled to 1280*711 >> add border to make it to 1280*712 >> no more MOD4 whining from SmoothLevels() :)

:thanks:

Gavino
27th October 2010, 14:06
I guess it all boils down to this post (http://forum.doom9.org/showthread.php?p=1453348#post1453348), would anyone know how to get it to work please?
-Vit-'s function, with IanB's correction, can be extended to work on 2.60 as follows:
function mmod(clip c, int modH, int modV)
{
bh = modH - ((c.Width()-1)%modH + 1)
bv = modV - ((c.Height()-1)%modV + 1)
l = bh/2
l = c.IsYUV() && l%2 != 0 ? l-1 : l
r = bh - l
t = bv/2
t = c.IsYV12() && t%2 != 0 ? t-1 : t
b = bv - t
c.AddBorders(l,t,r,b)
}

-Vit-
27th October 2010, 18:03
Or if you really need those odd sized borders, this modified AddBorders will allow anything as long as the result resolution is valid:

function AddBordersMod( clip c, int l, int t, int r, int b, int "color" )
{
l = (l>0)?l:0
t = (t>0)?t:0
r = (r>0)?r:0
b = (b>0)?b:0
color = default(color, 0)
w = c.Width()
h = c.Height()
lR = ((l+15)/16)*16
rR = ((r+15)/16)*16
tR = ((t+15)/16)*16
bR = ((b+15)/16)*16
c = c.AddBorders( lR, tR, rR, bR, color )
return c.BilinearResize( w+l+r, h+t+b, lR-l, tR-t, w+l+r, h+t+b )
}


It over-expands the borders then uses the more flexible cropping ability of the resizers (crops only - doesn't actually resize). However, this will necessarily resample the chroma on the formats it benefits.
[I tested this one ;)]

EDIT:
Might as well throw in the equivalent modified Crop, which allows odd values as long as the result is a valid resolution. Again this will resample to deal with the oddness:

function CropMod( clip c, int l, int t, int r, int b )
{
w = c.Width()
h = c.Height()
l = (l>0)?l:0
t = (t>0)?t:0
r = (r>0)?r:w+r
b = (b>0)?b:h+b
return c.BilinearResize( r-l, b-t, l, t, r-l, b-t )
}

dansrfe
27th October 2010, 18:26
So if I use this for YV12 video on a mod2 resolution to result in a mod2+ resolution then chroma will be resampled?

leeperry
27th October 2010, 18:36
ok, :thanks: guys! I'd rather avoid resizing/chroma resampling...I'd need MOD4 YV12 to remain untouched and non-MOD4 YV12 to be padded w/ a few pixels to avoid having SmoothLevels() complaining. It will then be fed to madVR. Having it centered when there's a need for 2 pixels for instance would be neat too, but I dunno if that'd be possible.

-Vit-
27th October 2010, 18:46
So if I use this for YV12 video on a mod2 resolution to result in a mod2+ resolution then chroma will be resampled?
If you set odd size borders, yes that's inevitable.
If you set even size borders the resizer is effectively just copying and the values might pass through unchanged. Surely would if you changed the last line to PointResize (although that change would be a bad choice for odd size borders)

leeperry
27th October 2010, 22:41
I think there's also the DAR problem, because ffdshow will tell madVR about the original DAR...but the movie dimensions have changed in the meantime :sly:

It might require to use (ffdshow_dar_x)/(ffdshow_dar_y) on the new dimensions to keep the OAR? http://leak.no-ip.org/AviSynth/ffdshow/docs/html/en/avisynth.htm

I will run it through some test patterns and report back.

dansrfe
27th October 2010, 23:45
personally speaking it'd probably better to just subtract one from the top and add one to the bottom in the script itself if the desired values are odd.

IanB
28th October 2010, 01:56
Note: 2.6 currently supports YV411 (ntsc dv), which subsamples the chroma horizontally by 4. So the AddBorders restriction can be mod 4.

The core internally supports 1, 2 & 4 subsampling both vertically and horizontally. I intend to expose these as generic planar with arbitrary subsampling.

-Vit-
28th October 2010, 03:21
The AddBordersMod above plays safe and assumes AddBorders can only do mod 16.

leeperry
25th August 2011, 22:30
-Vit-'s function, with IanB's correction, can be extended to work on 2.60 as follows:
function mmod(clip c, int modH, int modV)
{
bh = modH - ((c.Width()-1)%modH + 1)
bv = modV - ((c.Height()-1)%modV + 1)
l = bh/2
l = c.IsYUV() && l%2 != 0 ? l-1 : l
r = bh - l
t = bv/2
t = c.IsYV12() && t%2 != 0 ? t-1 : t
b = bv - t
c.AddBorders(l,t,r,b)
}

Hi again, this script does the job...but the borders often aren't black.

http://thumbnails38.imagebam.com/14668/9259f3146679364.jpg (http://www.imagebam.com/image/9259f3146679364) w/ "mmod(1,4)" ends up in http://thumbnails35.imagebam.com/14668/4e601d146679367.jpg (http://www.imagebam.com/image/4e601d146679367)

Any idea what's going wrong? Sometimes I have to change my upscaling resolution for the nasty horizontal bars to finally become black again, and even "mmod(1,16)" won't make them go away :o