View Single Post
Old 14th September 2015, 18:01   #14  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
Join Date: Jan 2004
Posts: 2,496
Here's an example explained in detail. It simulates the "zebra stripes" that some cameras show in the viewfinder to show out-of-range white values.

Code:
avisource("...")
levels(0,1,200,0,255,coring=false) # make some white values illegal for testing purposes

striped=last.y8_rpn("
x y + n + 128 * 1024 %
[y0]
dup 255 >
?")

mergechroma(striped.converttoyv12, last)
The first two lines just load the source and adjust the levels to make some of the luminance values illegal (higher than 235).

Next comes the RPN call. First, there's this line:

Code:
x y + n + 128 * 1024 %
This builds the stripey, animated background. It adds the pixel x coordinate, the pixel y coordinate, and the frame number, then multiplies the total by 128. It then takes the modulo of this with 1024 to make a repeating pattern of thin black stripes and thicker white stripes.

Code:
[y0]
Next, the luma value of the pixel is put on the stack. By default, y8_rpn expands the luma range from 16-235 to 0-255 on read. It doesn't clamp this value, though, so source pixels greater than 235 will leave a value greater than 255 on the stack.

Next:

Code:
dup 255 >
This line duplicates the [y0] value, because the comparison with 255 will remove the value from the stack, but we still want it on there. The comparison itself returns 1 if [y0] > 255, otherwise it returns 0.

Next:

Code:
?
Based on the previous result (1 or 0), this operator picks either the result of the first line, or the result of the second line (because these are the remaining two items on the stack; it has nothing to do with the placement of the newlines, which are for clarity only).

Finally a call to mergechroma restores the colour to the clip (rgba_rpn/y8_rpn ignore UV channels completely).

By this method, pixels which have an illegal luma value are replaced with the stripey pattern:


NB: because y8_rpn clamps output values to the legal range, by default, the output will no longer have any out-of-range luma pixels. I'm not sure this is the best way to behave, but it's always been a minefield.

Coming soon: loops and commenting.
__________________
My AviSynth filters / I'm the Doctor
wonkey_monkey is offline   Reply With Quote