/// <summary>
 /// Initializes the VapourSynth environment. It will be automatically finalized when the process exits.
 /// </summary>
 public static void Init()
 {
     if (!isInit)
     {
         // Increments on success, returns 0 on failure.
         if (VsInvoke.vsscript_init() > 0)
         {
             AppDomain.CurrentDomain.ProcessExit += (s, e) => {
                 // Decrements on success.
                 if (VsInvoke.vsscript_finalize() == 0)
                 {
                     isInit = false;
                 }
                 else
                 {
                     throw new Exception("Failed to finalize VapourSynth environment.");
                 }
             };
             isInit = true;
         }
         else
         {
             throw new Exception("Failed to initialize VapourSynth environment. 'vsscript.dll' could not be loaded.");
         }
     }
 }
        /// <summary>
        /// Creates an empty script environment.
        /// </summary>
        public static VsScript CreateEmpty()
        {
            IntPtr H = IntPtr.Zero;

            VsInvoke.vsscript_createScript(ref H);
            return(new VsScript(H));
        }
        /// <summary>
        /// Loads a script directly from a file. It will not be converted to COMPATBGR32.
        /// </summary>
        /// <param name="path">The path of the script file to open.</param>
        /// <param name="setWorkingDir">If true, current working directory will be changed to the path of the script.</param>
        public static VsScript LoadFileDirect(string path, bool setWorkingDir)
        {
            IntPtr H = IntPtr.Zero;

            if (VsInvoke.vsscript_evaluateFile(ref H, new Utf8Ptr(path).ptr, setWorkingDir ? VsInvoke.FlagSetWorkingDir : 0) == 0)
            {
                return(new VsScript(H));
            }
            else
            {
                string Err = new VsScript(H).GetError();
                VsInvoke.vsscript_freeScript(H);
                throw new VsException(Err);
            }
        }
        internal VsOutput(IntPtr scriptPtr, int nodeIndex)
        {
            getFrameAsyncCallback    = GetFrameAsync_Callback;
            getFrameAsyncCallbackPtr = Marshal.GetFunctionPointerForDelegate(getFrameAsyncCallback);
            this.scriptPtr           = scriptPtr;
            this.nodeIndex           = nodeIndex;

            nodePtr = VsInvoke.vsscript_getOutput(scriptPtr, nodeIndex);
            if (nodePtr == IntPtr.Zero)
            {
                throw new Exception("Failed to retrieve VapourSynth output node.");
            }

            this.apiPtr = VsInvoke.vsscript_getVSApi2(VsInvoke.VSSCRIPT_API_VERSION);
            if (this.apiPtr == IntPtr.Zero)
            {
                throw new Exception("Failed to get VapourSynth API.");
            }
            this.Api = Marshal.PtrToStructure <VsInvokeApi>(this.apiPtr);
        }
        /// <summary>
        /// Loads specified script.
        /// </summary>
        /// <param name="script">The script to load.</param>
        /// <param name="convertToCompatBgr32">If true, the script output will be converted to COMPATBGR32.</param>
        public static VsScript LoadScript(string script, bool convertToCompatBgr3)
        {
            if (convertToCompatBgr3)
            {
                script = AppendConvertCompoatScript(script);
            }

            IntPtr H = IntPtr.Zero;

            if (VsInvoke.vsscript_evaluateScript(ref H, new Utf8Ptr(script).ptr, IntPtr.Zero, 0) == 0)
            {
                return(new VsScript(H));
            }
            else
            {
                string Err = new VsScript(H).GetError();
                VsInvoke.vsscript_freeScript(H);
                throw new VsException(Err);
            }
        }
 public void Dispose()
 {
     VsInvoke.vsscript_clearOutput(apiPtr, nodeIndex);
     Api.freeNode(nodePtr);
 }
 /// <summary>
 /// Clears specified script video output.
 /// </summary>
 /// <param name="index">The output index specified in 'set_output'.</param>
 public int ClearOutput(int index)
 {
     return(VsInvoke.vsscript_clearOutput(handle, index));
 }
        /// <summary>
        /// Returns the error message returned by the script, or null.
        /// </summary>
        private String GetError()
        {
            IntPtr Err = VsInvoke.vsscript_getError(handle);

            return(Utf8Ptr.FromUtf8Ptr(Err));
        }
 /// <summary>
 /// Releases all resources associated with the script. Make sure all frames have been released first.
 /// </summary>
 public void Dispose()
 {
     VsInvoke.vsscript_freeScript(handle);
 }
 /// <summary>
 /// Returns the API version of the loaded Vapoursynth DLL.
 /// </summary>
 public static int GetApiVersion()
 {
     return(VsInvoke.vsscript_getApiVersion());
 }
 /// <summary>
 /// Loads VapourSynth DLLs from specified path. Call this before calling any other functions if DLLs can't be found by default.
 /// </summary>
 /// <param name="path">The path containing the DLLs.</param>
 public static void SetDllPath(string path)
 {
     VsInvoke.SetDllPath(path);
 }