Non-clip functions

From Avisynth wiki
Revision as of 22:33, 9 May 2013 by Admin (Talk | contribs)

Jump to: navigation, search

The input and output of these functions are not clips, but some other variables used in the script.

Contents

Numeric Functions

MulDiv(int, int, int) v2.56

(m * n + d / 2) / d with 64 bit intermediate result

Examples:

MulDiv (1, 1, 2) = 1
MulDiv (2, 3, 2) = 3

Floor(float)

Converts from float to int (round down on any fractional amount).

Examples:

Floor(1.2) = 1
Floor(1.6) = 1
Floor(-1.2) = -2
Floor(-1.6) = -2

Ceil(float)

Converts from float to int (round up on any fractional amount).

Examples:

Ceil(1.2) = 2.0
Ceil(1.6) = 2.0
Ceil(-1.2) = -1
Ceil(-1.6) = -1

Round(float)

Converts from float to int (round off to nearest integer).

Examples:

Round(1.2) = 1
Round(1.6) = 2
Round(-1.2) = -1
Round(-1.6) = -2

Sin(float) v2

Returns the sine of the argument (assumes it is radians).

Examples:

Sin(Pi()/4) = 0.707
Sin(Pi()/2) = 1.0

Cos(float) v2

Returns the cosine of the argument (assumes it is radians).

Examples:

Cos(Pi()/4) = 0.707
Cos(Pi()/2) = 0.0

Pi() v2

Returns the value of the "pi" constant (the ratio of a circle's perimeter to its diameter).

Examples:

d = Pi()    # d == 3.141593

Log(float) v2

Returns the natural (base-e) logarithm of the argument.

Examples:

Log(1) = 0.0
Log(10) = 2.30259

Exp(float) v2

Returns the natural (base-e) exponent of the argument.

Examples:

Exp(1) = 2.718282
Exp(0) = 1.0

Pow(float base, float power) v2

Returns "base" raised to the power indicated by the second argument.

Examples:

Pow(2, 3) = 8
Pow(3, 2) = 9
Pow(3.45, 1.75) = 8.7334

Sqrt(float) v2

Returns the square root of the argument.

Examples:

Sqrt(1) = 1.0
Sqrt(2) = 1.4142

Abs(float or integer) v2.07

Returns the absolute value (returns float for float, integer for integer).

Example:

Abs(-3.8) = 3.8

Sign(float) v2.07

Returns sign of value (1, 0 or -1).

Examples:

Sign(-3.5) = -1
Sign(3.5) = 1
Sign(0) = 0

Int(float) v2.07

Converts from float to int (round towards zero).

Examples:

Int(1.2) = 1
Int(1.6) = 1
Int(-1.2) = -1
Int(-1.6) = -1

Frac(float) v2.07

Returns the fractional portion of the value provided.

Examples:

Frac(3.7) = 0.7
Frac(-1.8) = -0.8

Float(int) v2.07

Converts int to float.

Value(string) v2.07

Converts string to numeric value.

Example:

Value ("-2.7") = -2.7

HexValue(string) v2.07

Evaluates string as hexadecimal value.

Example:

HexValue ("FF00") = 65280

Rand([int max] [, bool scale] [, bool seed]) v2.07

Returns a random integer value and all parameters are optional.

max sets the maximum value+1 (default 32768) and can be set negative for negative results. It operates either in scaled or modulus mode (default scale=true only if abs(max) > 32768, false otherwise).

Scaled mode (scale=true) scales the internal random number generator value to the maximum value, while modulus mode (scale=false) uses the remainder from an integer divide of the random generator value by the maximum. I found modulus mode is best for smaller maximums.

Using seed=true seeds the random number generator with the current time. seed defaults to false and probably isn't necessary, although it's there just in case.

Typically, this function would be used with the Select function for random clips.

Example:

Select(Rand(5), clip1, clip2, clip3, clip4, clip5)

Spline(float X, x1, y1, x2, y2, .... [, bool cubic]) v2.51

Interpolates the Y value at point X using the control points x1/y1, ... There have to be at least 2 x/y-pairs. The interpolation can be cubic (the result is a spline) or linear (the result is a polygon).

Examples:

Spline(5, 0, 0, 10, 10, 20, 0, false) = 5
Spline(5, 0, 0, 10, 10, 20, 0, true) = 7

String Functions

LCase(string) v2.07

Returns lower case of string.

Example:

LCase("AviSynth") = "avisynth"

UCase(string) v2.07

Returns upper case of string.

Example:

UCase("AviSynth") = "AVISYNTH"

StrLen(string) v2.07

Returns length of string.

Example:

StrLen("AviSynth") = 8

RevStr(string) v2.07

Returns string backwards.

Example:

RevStr("AviSynth") = "htnySivA"

LeftStr(string, int) v2.07

Returns first int number of characters.

Example:

LeftStr("AviSynth", 3) = "Avi"

RightStr(string, int) v2.07

Returns last int number of characters.

Example:

RightStr("AviSynth", 5) = "Synth"

MidStr(string, int pos [, int length]) v2.07

Returns substring starting at pos for optional length or to end. pos=1 specifies start.

Example:

MidStr("AviSynth", 3, 2) = "iS"

FindStr(string, substring) v2.07

Returns position of substring within string. Returns 0 if not found.

Example:

Findstr("AviSynth", "syn") = 4

String(float / int) v2

Converts a number to a string.

Example:

Subtitle( "Clip height is " + String(last.height) )

Chr(int) v2.51

Returns the ASCII character.

Example:

Chr(34) returns the quote character

Time(string) v2.51

Returns a string with the current system time formatted as defined by the string.Codes for output formatting:

%a Abbreviated weekday name
%A Full weekday name
%b Abbreviated month name
%B Full month name
%c Date and time representation appropriate for locale
%d Day of month as decimal number (01 ? 31)
%H Hour in 24-hour format (00 ? 23)
%I Hour in 12-hour format (01 ? 12)
%j Day of year as decimal number (001 ? 366)
%m Month as decimal number (01 ? 12)
%M Minute as decimal number (00 ? 59)
%p Current locale?s A.M./P.M. indicator for 12-hour clock
%S Second as decimal number (00 ? 59)
%U Week of year as decimal number, with Sunday as first day of week (00 ? 53)
%w Weekday as decimal number (0 ? 6; Sunday is 0)
%W Week of year as decimal number, with Monday as first day of week (00 ? 53)
%x Date representation for current locale
%X Time representation for current locale
%y Year without century, as decimal number (00 ? 99)
%YYear with century, as decimal number
%z, %Z Time-zone name or abbreviation; no characters if time zone is unknown
%% Percent sign 

The # flag may prefix any formatting code. In that case, the meaning of the format code is changed as follows:

%#a, %#A, %#b, %#B, %#p, %#X, %#z, %#Z, %#%
No change; # flag is ignored. 
%#c 
Long date and time representation, appropriate 
for current locale. For example: 
  ?Tuesday, March 14, 1995, 12:41:29?. 
%#x 
Long date representation, appropriate to current 
locale. For example: 
  ?Tuesday, March 14, 1995?. 
%#d, %#H, %#I, %#j, %#m, %#M, %#S, %#U, %#w, %#W, %#y, %#Y 
Remove leading zeros (if any).

Version Checking Functions

VersionNumber() v2.07

Returns AviSynth version number as float.

Example:

VersionNumber() = 2.07

VersionString() v2.07

Returns AviSynth version info as string (first line used in Version command).

Example:

VersionString() = "AviSynth 2.08 (avisynth.org) 22 nov. 2002"

Variable Type and File Checking Functions

IsBool(var)

IsInt(var)

IsFloat(var)

IsString(var)

IsClip(var)

Exist(filename) v2.07

Checks if specified file exists.

Defined(var): returns true if var defined, false otherwise.

Default(x,d): returns x if Defined(x), d otherwise.

Note: Defined and Default are primarily designed for dealing with optional declared variables in user-defined functions and are used to determine whether an optional variable has been passed. Any completely undeclared variable will generate an error. See "User Defined Functions" later in this section for more information.

Eval(string)

Apply(func-string, arg, ...): Eval("f(x)") is equivalent to f(x) is equivalent to Apply("f", x))

You can use Eval for something like (here a specific call to BicubicResize filter is shown, but you get the idea):

settings = "352, 288"
Eval( "BicubicResize(" + settings + ")" )

You can import the text of another script:

Import (filename): evals contents of another 	AviSynth script.

For error reporting and catching bad input to user-defined function you can use (error-message if bool=false): Assert (bool, string error-message)

There is a function for checking if an error WILL arise:

try {
  AviSource("file.avi")
}
catch(err_msg) {
   BlankClip.Subtitle(err_msg)
}

Control Functions

SetMemoryMax (int): Sets the maximum memory (in MB) that AviSynth uses for its internal Video Frame cache

From v2.5.8, setting this to zero just returns the current Memory Max value. In the 2.5 series the default Memory Max value is 25% of the free physical memory, with a minimum of 16MB. From rev 2.5.8 RC4, the default Memory Max is also limited to 512MB.


SetPlanarLegacyAlignment (clip, bool): Set alignment mode for planar frames. v2.5.6

Some older plugins illegally assume the layout of video frames in memory. This special filter forces the memory layout of planar frames to be compatible with prior version of AviSynth. The filter works on the GetFrame() call stack, so it effects filters before it in the script.

Example:

# Using an older version of Mpeg2Source()
LoadPlugin("...\Mpeg2Decode.dll")
Mpeg2Source("test.d2v")         # A plugin that illegally assumes 
                                # the layout of memory
SetPlanarLegacyAlignment(true)  # Set legacy memory alignment for prior statements
ConvertToYUY2()                 # Statements thru to the end of the script have
...                             # advanced memory alignment.

SetWorkingDir (string): Sets the directory from which AviSource, LoadPlugin, etc. are referenced. This is primarily for easy loading of source clips, etc. Does not affect plugin autoloading. Return value: 0 if successful, -1 otherwise. v2

Multi threading:

These functions enable avisynth to use more than one thread when processing filters. This is useful if you have more than one cpu/core or hyperthreading. This feature is still experimental.

GetMTMode(bool threads). v.2.6 threads: If true GetMTMode returns the number of threads used else the current mode is returned (see below). Default value false.

SetMTmode(int mode,int threads). v2.6

Place this at the first line in the avs file to enable temporal (that is more than one frame is processed at the same time) multithreading. Use it later in the script to change the mode for the filters below it.

mode: There are 6 modes 1 to 6. Default value 2.

  • Mode 1 is the fastest but only works with a few filter.
  • Mode 2 should work with most filters but uses more memory.
  • Mode 3 should work with some of the filters that doesn't work with mode 2 but is slower.
  • Mode 4 is a combination of mode 2 and 3 and should work with even more filter but is both slower and uses more memory
  • Mode 5 is slowest but should work with all filters that doesn't require linear frameserving (that is the frames come in order (frame 0,1,2 ... last).
  • Mode 6 is a modified mode 5 that might be slightly faster.

threads: Number of threads to use. Set to 0 to set it to the number of processors available. It is not possible to change the number of threads other than in the first SetMTMode. Default value 0.

Example:

SetMTMode(2,0)                    #enables multihreading using thread = to 
                                  #the number of available processors and mode 2
LoadPlugin("...\LoadPluginEX.dll") #needed to load avisynth 2.0 plugins
LoadPlugin("...\DustV5.dll")       #Loads Pixiedust
Import("limitedsharpen.avs") 
src=AviSource("test.avi")
SetMTMode(5)                       #change the mode to 5 for the lines below
src=src.converttoyuy2().PixieDust()#Pixiedust needs mode 5 to function.
SetMTMode(2)                       #change the mode back to 2
src.LimitedSharpen()               #because LimitedSharpen works well with mode 2 
#display mode and number of threads in use
Subtitle("Number of threads used: " + String(GetMTMode(true))
 \ + " Current MT Mode: " + String(GetMTMode()))


Conditional Operations:

```[```var```=]```boolean expression ? iftrue value or operation : ifelse value or operation

nop(): v2.07 null result primarily for if-then-else operation above where ifelse is not desired

(nop primarily to be used against non-variable / non-clip functions such as import and loadplugin)

Example:

 VersionNumber<2.07 ? Import("patches.avs") : nop()

Select(int index, val item0 ```[```,item1...```]```) <v2.07>

Indexed selection of item0 ... item<n-1> (no particular limit on number of items). Items can be of any type, including clips and technically, item types don't have to match, but that could be problematic. Can be used with Rand() function for the index to have a random clip generator or to manage various versions (i.e.,title, preview, main or perhaps PAL, NTSC, FILM) in the same script.

User defined functions:

You can define and call your own functions in AVS scripts as shown below. Script Grammar within a function is identical to that of a script at large. The function can return any clip or variable type. The format is generally as follows:

function function_name(```[```var_type var_name ```[,...]```)
{
    function commands
    .
    .
    .
    return(result)
}

Curled brackets must enclose the function commands, but they can share lines with the function header and / or the first and last function commands. The following is an example of a single line function:

function rednum(clip c, int "num") {
    return Subtitle(c,string(default(num,0)),text_color=$FF0000)
}

Variables passed to functions can be made optional by enclosing var_name in quotes (i.e., int "num" in the previous example). Within the function, you would include a Defined or Default function to determine if var_name was passed and set the default value or alter your processing accordingly. Optional parameters can also be called using the var_name=value syntax to skip over other optional parameters.

function NTSC2PAL(clip c) {
    # Fairly good NTSC->PAL conversion.  Would be better with Smart Bob. :-)
    Assert(c.height == 480, "NTSC2PAL: input clip must have 480 scan lines")
    Bob(c, height=576)
    ChangeFPS(50)
    SeparateFields.SelectEvery(4, 0, 3)
    return Weave
}

AviSource("ntsc.avi").NTSC2PAL

If you create anything cool, be sure to post it to Shared Script Functions!

Personal tools