Postfix
VapourSynth Expr Syntax Documentation¶
Expr is a powerful VapourSynth function that evaluates a per-pixel mathematical or logical expression. Its core is the expr string, which uses Reverse Polish Notation (RPN). This guide details the syntax of that string.
1. Core Concepts¶
1.1. Reverse Polish Notation (RPN)¶
Instead of the conventional A + B, RPN places operators after their operands: A B +. The expression is evaluated using a stack. Values are pushed onto the stack, and operators pop values, perform a calculation, and push the result back onto the stack.
- Example: To calculate
(5 + 3) * 2, the RPN expression is5 3 + 2 *. 5: Push 5. Stack:[5]3: Push 3. Stack:[5, 3]+: Pop 3 and 5, calculate 8, push 8. Stack:[8]2: Push 2. Stack:[8, 2]*: Pop 2 and 8, calculate 16, push 16. Stack:[16]
The final value on the stack is the result for the pixel.
1.2. Data Ranges¶
Expr does not normalize input clip values. You must account for the native range of the pixel format.
- 8-bit integer: 0 to 255
- 10-bit integer: 0 to 1023
- 16-bit integer: 0 to 65535
- 32-bit float: Typically 0.0 to 1.0 for Luma (Y) and Alpha, and -0.5 to 0.5 for Chroma (U/V).
When mixing formats, scale values accordingly. For example, to add an 8-bit value to a 10-bit value, you might use x y 4 * + (multiplying the 8-bit value y by 4 to scale it to the 10-bit range).
2. std.Expr Base Syntax¶
This section covers the standard operators and identifiers available in std.Expr.
2.1. Clip Identifiers¶
Input clips are referred to by letters. Up to 26 clips can be used.
x: The first input clip.y: The second input clip.z: The third input clip.atow: The 4th to 26th clips.
2.2. Constants¶
Numeric literals are pushed directly onto the stack.
- Example:
x 128 -(Subtracts 128 from each pixel value of the first clip).
2.3. Arithmetic Operators (2 operands)¶
+: Addition-: Subtraction*: Multiplication/: Division
2.4. Math Functions¶
pow: (2 operands)x y powraisesxto the power ofy.exp: (1 operand)x expis e^x.log: (1 operand)x logis the natural logarithm ofx.sqrt: (1 operand)x sqrtis the square root ofx.abs: (1 operand)x absis the absolute value ofx.sin: (1 operand)x sinis the sine of x.cos: (1 operand)x cosis the cosine of x.
2.5. Comparison & Logical Operators (2 operands)¶
These operators treat any value greater than 0 as true. They return 1.0 for true and 0.0 for false.
>: Greater than<: Less than=: Equal to>=: Greater than or equal to<=: Less than or equal toand: Logical ANDor: Logical ORxor: Logical XORnot: (1 operand) Logical NOT.
2.6. Conditional Operator (3 operands)¶
?: A ternary operator,C A B ?is equivalent toC ? A : B. IfCis true (non-zero),Ais evaluated and its result is pushed. Otherwise,Bis evaluated and its result is pushed.- Example:
x 128 > x 0 ?(If the pixel value is greater than 128, keep it, otherwise set it to 0).
2.7. Min/Max Operators (2 operands)¶
max: Returns the larger of the two values.min: Returns the smaller of the two values.
2.8. Stack Manipulation Operators¶
dup: (1 operand) Duplicates the top item on the stack.x dup *is equivalent tox x *orx 2 pow.swap: (2 operands) Swaps the top two items on the stack.x y swap -is equivalent toy x -.dupN: Duplicates the item at N positions from the top of the stack.dup0isdup.swapN: Swaps the top item with the item N positions down the stack.swap1isswap.
3. akarin.Expr Extended Syntax¶
akarin.Expr includes all std.Expr functionality plus the following additions.
3.1. Special Variables & Constants¶
These are special operators that push a specific value onto the stack.
pi: Pushes the mathematical constant π (approximately 3.14159).N: Pushes the current frame number.X: Pushes the current pixel's column coordinate (width).Y: Pushes the current pixel's row coordinate (height).width: Pushes the width of the frame.height: Pushes the height of the frame.
3.2. Frame Property Access¶
clip.PropertyName: Loads a scalar numerical frame property.- Example:
x.PlaneStatsAveragepushes the average pixel value of the current plane from the first clip. If the property doesn't exist, the value isNaN.
3.3. Additional Math & Logic¶
**: An alias forpow.x 2 **is the same asx 2 pow.%: Implements C'sfmodf.x 1.0 %gives the fractional part ofx.clip/clamp: (3 operands) Clamps a value.x min max clipis equivalent tox max min.- Example:
x 16 235 clipclamps the pixel value to the range. trunc: (1 operand) Truncates the value towards zero.round: (1 operand) Rounds the value to the nearest integer.floor: (1 operand) Rounds the value down to the nearest integer.
3.4. Advanced Stack Manipulation¶
- Named Variables:
var!: Pops the top value from the stack and stores it in a variable namedvar.var@: Pushes the value of the variablevaronto the stack.- Example:
x 2 / my_var! my_var@ my_var@ *(Calculates(x/2)^2). dropN: Drops the topNitems from the stack.dropis an alias fordrop1.- Example:
1 2 3 drop2results in a stack of[1]. sortN: Sorts the topNitems on the stack, with the smallest value ending up on top.- Example:
3 1 2 sort3results in a stack of[3, 2, 1], with1at the top.
3.5. Pixel Access¶
- Static Relative Access:
clip[relX, relY] - Accesses a pixel relative to the current coordinate.
relXandrelYmust be constants. - Example:
y[-1, 0]accesses the pixel to the immediate left in the second clip (y). - Boundary Suffixes:
:c: Clamped boundary (edge pixels are repeated). This is the default.x[relX, relY]:c.:m: Mirrored boundary.x[relX, relY]:m.
- Dynamic Absolute Access:
absX absY clip[] - Accesses a pixel at an absolute coordinate.
absXandabsYcan be computed by expressions. This is more flexible but potentially slower. - Example:
X 2 / Y x[]reads the pixel at half the current X coordinate from the first clip.
Note:
X 2 + Y 3 - x[]is equal tox[2,-3].
3.6. Bitwise Operators¶
These operators work on integers. Set opt=1 in the function parameters to ensure integer evaluation for clips with more than 24 bits.
bitand: Bitwise AND.bitor: Bitwise OR.bitxor: Bitwise XOR.bitnot: Bitwise NOT.
3.7. Extended Numeric Formats¶
- Hexadecimal:
0x10(16),0xFF(255). - Octal:
010(8). Note that invalid octal numbers like09are parsed as floats (9.0).
3.8. Arbitrary Number of Clips¶
srcN: Accesses the N-th input clip (0-indexed).src0is equivalent tox.src1is equivalent toy.src26accesses the 27th clip.