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 14th October 2012, 00:35   #1  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
Join Date: Jan 2004
Posts: 2,493
xyremap - reverse Polish pixel remapper (experimental)

Download beta: xyremap0.5beta.zip v0.5 (beta)
version 0.5: some new RPN functions, a bug fix, and (approximately) gamma-aware interpolation (see http://forum.doom9.org/showthread.ph...01#post1736501)
version 0.4: new x86 RPN compiler; new functionality (see http://forum.doom9.org/showthread.ph...05#post1735605)

Download: xyremap0.3.zip v0.3
version 0.3: new parameter "static" to speed up non-animating remaps
version 0.2: bug fixes to pixel interpolator, min/max functions, added new RPN parameters

See included xyremap.txt for parameters, etc.
See here and here for explanations of Reverse Polish Notation

Please note that the RPN parser is not very intelligent when it comes to malformed notation, and may simply return an unexpected result without erroring.

Examples

To make some animated waves:

Code:
xyremap(version.converttorgb32,y="y x 0.1 * n 5 / + sin 8 * +")


To take a trip into the time vortex:

Code:
stackhorizontal(version,version)
stackvertical(last,last)
converttorgb32
xyremap(\
"y h 0.5 * - x w 0.5 * - atan2 n 250 / + TAU + u 1.5 * TAU / * u 0.5 * % u 0.25 * +",\
"1 y h 0.5 * - 2 ^ x w 0.5 * - 2 ^ + sqrt / w 100 * * n + v 0.5 * % v 0.25 * +",\
"y h 0.5 * - 2 ^ x w 0.5 * - 2 ^ + sqrt w 0.5 * /",\
w=640,\
h=360,\
draft=false)
(four "versions" are stacked so the remapper can avoid the image edges, which would otherwise result in some visible edges on the output)


Last edited by wonkey_monkey; 30th August 2015 at 18:59.
wonkey_monkey is offline   Reply With Quote
Old 18th October 2012, 19:13   #2  |  Link
ajp_anton
Registered User
 
ajp_anton's Avatar
 
Join Date: Aug 2006
Location: Stockholm/Helsinki
Posts: 805
Whoa, this is just what I was requesting a short while ago, but then gave up that project...
Would love to get some time to play around with this, looks like it can do pretty much everything =).


"Note that "-" is used instead of "+" because we are mapping from output to input pixels, not the other way around (which would be more intuitive)."
I'm guessing it's like this so that each output pixel will have an input location (interpolated pixel) to get its data from.

"t: t=n/framecount (so 0<=0<1)"
0<=t<1
ajp_anton is offline   Reply With Quote
Old 18th October 2012, 22:40   #3  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
Join Date: Jan 2004
Posts: 2,493
Quote:
Originally Posted by ajp_anton View Post
"Note that "-" is used instead of "+" because we are mapping from output to input pixels, not the other way around (which would be more intuitive)."
I'm guessing it's like this so that each output pixel will have an input location (interpolated pixel) to get its data from.
Exactly.

Quote:
"t: t=n/framecount (so 0<=0<1)"
0<=t<1
Hmm, well spotted. Although 0<=0<1 is also true

David
wonkey_monkey is offline   Reply With Quote
Old 1st November 2012, 13:58   #4  |  Link
ajp_anton
Registered User
 
ajp_anton's Avatar
 
Join Date: Aug 2006
Location: Stockholm/Helsinki
Posts: 805
Am I the only one who finds this interesting?

Feature requests:
- Ability to choose the color of the padding outside the input image, or extend the existing border pixels. The bicubic interpolation is making my stretched edges dark.
- Boolean to make x and y go from 0 to 1 so I don't have to divide by u and v.
- Is it possible to make the function go the other direction, map input pixels to the output image instead? It's difficult enough to find the function from input to output that then needs to be reverseed/solved with WolframAlpha, but my functions are often non-solvable =). And it would be easier to wrap helper functions around them.
ajp_anton is offline   Reply With Quote
Old 1st November 2012, 17:51   #5  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
Join Date: Jan 2004
Posts: 2,493
Quote:
Originally Posted by ajp_anton View Post
Am I the only one who finds this interesting?
Apparently

Quote:
Feature requests:
- Ability to choose the color of the padding outside the input image
This is probably best left outside of xyremap's remit. The alpha channel is available so you can comp in a different background with overlay.

Quote:
extend the existing border pixels. The bicubic interpolation is making my stretched edges dark.
This can be done either by stacking multiple versions of your input together before remapping (or padding with background colour), then using the % (modulo) operator to constrain the input pixels to within the "real" borders. Or you could try adding:

Code:
... 0.5 max w 0.5 - min
... 0.5 max h 0.5 - min
to your x and y expressions respectively. This may or may not solve it though - I can't test it properly because I've just discovered a bug in the interpolation function that seems to be messing up levels.

Quote:
- Boolean to make x and y go from 0 to 1 so I don't have to divide by u and v.
Rather than a bool, how about two new parameters, a and b, for these values?

Quote:
- Is it possible to make the function go the other direction, map input pixels to the output image instead?
What happens if two input pixels map to the same output pixel? Also bicubic interpolation that way around would be tricky (if not impossible).



TL;DR: there is a bug in the interpolator that I need to fix which is producing slightly wrong pixel values.

David

Last edited by wonkey_monkey; 1st November 2012 at 18:05.
wonkey_monkey is offline   Reply With Quote
Old 1st November 2012, 19:02   #6  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
Join Date: Jan 2004
Posts: 2,493
xyremap v0.2

A couple of bugs fixed - min/max weren't working properly, and the pixel interpolation bug is fixed - and some new RPN parameters are added:

Code:
	a: pixel x coordinate normalised to [0-1]
	b: pixel y coordinate normalised to [0-1]
	c: pixel y coordinate normalised to [0-1*display_aspect_ratio]
You'll still have to multiply by w/h at the end though - I may look at this again sometime, but at least it saves you the more costly divisions.

Hope that helps!

David
wonkey_monkey is offline   Reply With Quote
Old 2nd November 2012, 20:06   #7  |  Link
ajp_anton
Registered User
 
ajp_anton's Avatar
 
Join Date: Aug 2006
Location: Stockholm/Helsinki
Posts: 805
Thanks.
Yes it might be a problem with mapping into the same, but my functions don't do that =). Oh well, I guess I can live without it.

I padded it myself using pointresize (needed 2 pixels so the last pixel wouldn't be slightly dark), but I would've liked for an easier solution. The modulo solution looks interesting though, didn't think of that.

Another request: an "if" statement.

Last edited by ajp_anton; 2nd November 2012 at 20:12.
ajp_anton is offline   Reply With Quote
Old 2nd November 2012, 20:39   #8  |  Link
mastrboy
Registered User
 
Join Date: Sep 2008
Posts: 365
Quote:
Originally Posted by ajp_anton View Post
Am I the only one who finds this interesting?
The plugin functions in itself is interesting, but as probably many others I only use avisynth to correct "errors" in the video, like f eks rainbowing and dotcrawl.

My guess is that those who want to manipulate video and add effects turn to Adobe AfterEffects or similar.
__________________
(i have a tendency to drunk post)
mastrboy is offline   Reply With Quote
Old 6th November 2012, 18:56   #9  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
Join Date: Jan 2004
Posts: 2,493
Quote:
Originally Posted by mastrboy View Post
The plugin functions in itself is interesting, but as probably many others I only use avisynth to correct "errors" in the video
Perhaps my examples were a little esoteric, but of course xyremap could be put to use correcting picture distortions and the like. Perhaps a YUV version would be useful for correcting some chroma misalignments.

Quote:
My guess is that those who want to manipulate video and add effects turn to Adobe AfterEffects or similar.
Which is not, contrary to popular belief, a free download so now (for very simple things) they don't need to.

Anyway, here's another frivolous (and very slow) use of xyremap:

Code:
a=blankclip(width=640,height=1280,length=1500).subtitle(\
"It   is  a   period  of   civil  war.\n"+\
"Rebel    spaceships,    striking\n"+\
"from a hidden base, have won\n"+\
"their    first    victory    against\n"+\
"the   evil   Galactic   Empire.   \n\n"+\
"During    the    battle,     Rebel\n"+\
"spies managed to steal secret\n"+\
"plans      to      the      Empire's\n"+\
"ultimate  weapon,  the DEATH\n"+\
"STAR,    an    armored    space\n"+\
"station with enough power to\n"+\
"destroy  an  entire  planet.      \n\n"+\
"Pursued    by    the    Empire's\n"+\
"sinister     agents,     Princess\n"+\
"Leia  races  home  aboard her\n"+\
"starship,   custodian   of   the\n"+\
"stolen  plans  that  can  save\n"+\
"her     people     and     restore\n"+\
"freedom  to  the  galaxy....      "\
,lsp=0,align=8,size=50).converttorgb32

b=a.reduceby2.bicubicresize(320,320)
c=b.reduceby2.bicubicresize(160,80)
d=c.reduceby2.bicubicresize(80,20)

a=a.xyremap("x w 0.5 * - 1 y h / * / u w / * u 0.5 * +","v h y - y / h * n 4 * - v + - v 1280 / *","y 4 - h / sqrt 1 h y - 0.005 * - *",w=640,h=360)
b=b.xyremap("x w 0.5 * - 1 y h / * / u w / * u 0.5 * +","v h y - y / h * n 4 * - v + - v 1280 / *","y h / sqrt h y - 0.005 * 1 180 y - 0.01 * - min *",w=640,h=360)
c=c.xyremap("x w 0.5 * - 1 y h / * / u w / * u 0.5 * +","v h y - y / h * n 4 * - v + - v 1280 / *","y h / sqrt 180 y - 0.01 * 1 90 y - 0.02 * - min *",w=640,h=360)
d=d.xyremap("x w 0.5 * - 1 y h / * / u w / * u 0.5 * +","v h y - y / h * n 4 * - v + - v 1280 / *","y h / sqrt 90 y - 0.02 * *",w=640,h=360)

e=overlay(a,b,mode="add",pc_range=true)
f=overlay(c,d,mode="add",pc_range=true)
overlay(e,f,mode="add",pc_range=true)


Tell me that's not practical

David
wonkey_monkey is offline   Reply With Quote
Old 6th November 2012, 20:53   #10  |  Link
jmac698
Registered User
 
Join Date: Jan 2006
Posts: 1,867
I think this would be useful to remove barrel distortion from a camera lens. Once you do that, it's simple to stitch together scenes into panoramas.
The other problems though, could be vignetting and exposure changes, but in some cases it could work.
jmac698 is offline   Reply With Quote
Old 6th November 2012, 22:27   #11  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
This is quite spectacular, limited usage as Mastrboy suggests, but impressive, just the same.
Think maybe the fascination with Dr Who may have influenced the vortex-ish type intro.

EDIT: PS, nice work.
__________________
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 November 2012 at 22:29.
StainlessS is offline   Reply With Quote
Old 7th November 2012, 02:51   #12  |  Link
jmac698
Registered User
 
Join Date: Jan 2006
Posts: 1,867
Really? Vortices are a common demo effect
scene.org
I'd like to make another avisynth demo
http://forum.doom9.org/archive/index.php/t-163121.html
jmac698 is offline   Reply With Quote
Old 7th November 2012, 09:58   #13  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
Join Date: Jan 2004
Posts: 2,493
Quote:
Think maybe the fascination with Dr Who may have influenced the vortex-ish type intro.
Quote:
Really? Vortices are a common demo effect
No, StainlessS is right, though how he could possibly know I like Doctor Who is a mystery...
wonkey_monkey is offline   Reply With Quote
Old 20th November 2012, 22:25   #14  |  Link
martin53
Registered User
 
Join Date: Mar 2007
Posts: 407
David,

I'd like to suggest some extensions that I hope are manageable to program, yet extend the capabilities significantly.

A) Extension of RPN
  • dup, unary operator, duplicates top operand for later
    re-use
  • sign, unary operator for sign (-1 for <0, 0 for 0, 1 for >0)
  • round, floor, ceil, converting float to integer like AviSynth does
  • log, exp, logarithm and exponent

B) Extension of input variables
r/g/b/a red/green/blue/alpha pixel values

C) Extension of parameters
r/g/b/a red/green/blue/alpha pixel values additionally to x/y coordinates - i understand that xyremap is made with two cascaded loops, one over the lines and one over the columns? so, with the extensions b) and c), instead of remapping, xyremap could be used as color manipulation function beyond YV12LUT() for RGB format.

D) A background clip, and dx, dy parameters to overlay output with offset over it - considering alpha, of course.

E) new name xyrgbaSwissKnife
martin53 is offline   Reply With Quote
Old 21st November 2012, 00:44   #15  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
Join Date: Jan 2004
Posts: 2,493
Hi Martin,

I had considered some extensions. I'm not sure what you mean about dup - how would you "re-use" the duplicated operand? I considered an assignment operator, =, to set your own variables (and perhaps then an operator to reset the current stack as well, if required, leaving assigned variables intact), but that means extra checks to make sure the hard-coded variables aren't overwritten. Perhaps also some comparison operators - lt, gt, eq - and some way to implement if ... then ... else in RPN.

As for B) and C), which r/g/b/a pixel values would these be? The filter is all about calculating x,y coordinates of source pixels, so how can it know which pixel to read to get the RGBA values? I'm also not sure what you mean by "additionally to x/y coordinates" in C).

A separate RGBA filter is certainly possible, though.

D) I thought was best left to the user to use the internal layer filter.

E) Silly

David

Last edited by wonkey_monkey; 21st November 2012 at 00:49.
wonkey_monkey is offline   Reply With Quote
Old 21st November 2012, 12:05   #16  |  Link
ajp_anton
Registered User
 
ajp_anton's Avatar
 
Join Date: Aug 2006
Location: Stockholm/Helsinki
Posts: 805
"some way to implement if ... then ... else in RPN."

See how it's done in mt_masktools. You have the ==, >, <... checks that take two input variables and return true/false.
Then you have the ? that takes three inputs. One bool, one to return if it's true, one if it's false.
if a==b, then T, else F
a b == T F ?
ajp_anton is offline   Reply With Quote
Old 21st November 2012, 18:30   #17  |  Link
martin53
Registered User
 
Join Date: Mar 2007
Posts: 407
Quote:
Originally Posted by davidhorman View Post
Hi Martin,

I'm not sure what you mean about dup
- You are right: MaskTools also use RPN, and I feel it would be a good idea to use similar operand sets. I only used yv12lut so far. There is no min operator. To subtract a value, but avoid negative results, I write "x 128 - 0 < 0 x 128 - ?", or in human speech, "subtract 128 from x, and if the result is below zero, take zero, else take x minus 128". X-128 must be computed twice, and if I needed to limit a very complicated expression, I have no confidence that I could manage that (bug finding is not easy with RPN). I thought that with dup, an intermediate result could be stored and re-used more easily. But in fact it would be useless because the copy is needed only later and cannot easily handled on the RPN stack.

A different strategy would be needed. A limited set of 'variables'-operands, like 's0' stores top of stack to 'v0' and leaves it also on stack, 'r0' recalls 'v0' to top of stack would help - and stay within the syntax.

As a side thought: To facilitate bug finding, the function could throw an error if the expression does not end with 1 entry on the stack, and dump the stack. What do you think about this?
martin53 is offline   Reply With Quote
Old 21st November 2012, 19:26   #18  |  Link
martin53
Registered User
 
Join Date: Mar 2007
Posts: 407
Quote:
Originally Posted by davidhorman View Post
As for B) and C), which r/g/b/a pixel values would these be? The filter is all about calculating x,y coordinates of source pixels, so how can it know which pixel to read to get the RGBA values?
I see the concept. B) only makes sense in C).
For the RPN formulas of parameters "x" and "y", no extension is useful.
My suggestion meant four more parameters string "r", string "g", string "b", string "a", all with RPN expressions.
With these expressions, also the variables r, g, b, a of the source pixel would be fantastic (instead of copying it 1:1). Imagine the cool color gradients possible with just xyremapping a BlankClip, even x="x", y="y" and using the variables x,y in the brightness RPN expression (shiver). xyremap(x="x",y="y",r="x u 2 / - 2 ^ y v 2 / - 2 ^ + u 2 ^ v 2 ^ + / PI * cos") red = cos( PI * ((x-u/2)²+(y-v/2)²)/(u²+v²) )

Quote:
Originally Posted by davidhorman View Post
A separate RGBA filter is certainly possible, though.
It would be a pity if x and y would not be present as variables to define the brightness. No color gradients then.

Quote:
Originally Posted by davidhorman View Post
D) I thought was best left to the user to use the internal layer filter.
You allow an implicit cropping with the parameters w and h. You are right: even if an alpha value is calculated with xyremap, the output clip can still be forwarded to layer. I tend to create complicated processing. Because long graphs use much memory, I prefer few powerful filters over many simple ones. Only because of that, I phantasized to combine the two steps into one.
martin53 is offline   Reply With Quote
Old 21st November 2012, 23:15   #19  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
Join Date: Jan 2004
Posts: 2,493
Quote:
As a side thought: To facilitate bug finding, the function could throw an error if the expression does not end with 1 entry on the stack, and dump the stack. What do you think about this?
What I think is that it already does this

Code:
compiler error - check your string (x) # where x is how many values are left on the stack

Code:
x 128 - 0 < 0 x 128 - ?
In my RPN machine, this can be written:

Code:
x 128 - 0 min
as you know, but with user variables it could possibly be written:

Code:
x 128 - q= 0 < 0 q ?
The "q=" would have to be a pseudo-operator, since it doesn't really work like RPN should (and it has no effect on the stack - a stack reset operator could also be useful), but it would be useful.

Quote:
You allow an implicit cropping with the parameters w and h
True, but that's for speed reasons. No sense doing all those heavy calculations only to crop them away.

Quote:
Imagine the cool color gradients possible with just xyremapping a BlankClip, even x="x", y="y" and using the variables x,y in the brightness RPN expression (shiver).
I don't see any sensible interpretation for the rgba values except when using x="x" and y="y" - in which case it may as well be a separate filter (maybe in the same DLL, but a separate keyword - rgbaremap). Plus, I already used the variables a and b

Warping an image in two dimensions is hard enough - warping it in 6 is crazy!

David

PS x="x" and y="y" are the defaults for those expressions, so don't need to be specified.

Last edited by wonkey_monkey; 21st November 2012 at 23:36.
wonkey_monkey is offline   Reply With Quote
Old 22nd November 2012, 00:09   #20  |  Link
Didée
Registered User
 
Join Date: Apr 2002
Location: Germany
Posts: 5,389
Quote:
Originally Posted by martin53 View Post
Imagine the cool color gradients possible with just xyremapping a BlankClip, even x="x", y="y" and using the variables x,y in the brightness RPN expression (shiver).
MaskTools v2: "mt_lutspa()"
__________________
- We´re at the beginning of the end of mankind´s childhood -

My little flickr gallery. (Yes indeed, I do have hobbies other than digital video!)
Didée 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 14:57.


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