// utility functions // Opens AVS file and sets some member variables. void open(const char* avsfile) { DBGLOG("avsutil::impl::cavs_type::open(\"" << avsfile << "\")"); try { // pack the filename as the argument of AviSynth filter AVSValue filename = avsfile; AVSValue args = AVSValue(&filename, 1); // load AviSynth script AVSValue imported = mv_se->Invoke("Import", args, 0); // get the clip and video informations mv_clip = imported.AsClip(); // store filename mv_filepath = avsfile; } catch (AvisynthError& avserr) { mv_is_fine = false; mv_errmsg = avserr.msg; } catch (std::exception& ex) { mv_is_fine = false; mv_errmsg = ex.what(); } }
int main() { try { cout << "Creating script environment 1..." << endl; IScriptEnvironment* env1 = CreateScriptEnvironment(3); cout << "Creating script environment 2..." << endl; IScriptEnvironment* env2 = CreateScriptEnvironment(3); cout << "Deleting script environment 1..." << endl; delete env1; cout << "Invoking BlankClip on env 2..." << endl; AVSValue ret = env2->Invoke("BlankClip", AVSValue(), 0); PClip clp = ret.AsClip(); cout << "Reading frame 0 from env2..." << endl; PVideoFrame frm = clp->GetFrame(0, env2); } catch (AvisynthError &e) { cerr << "AvisynthError: " << e.msg << endl; return -1; } catch (...) { cerr << "unknown error" << endl; return -1; } return 0; }
AVSValue ExpConditional::Evaluate(IScriptEnvironment* env) { AVSValue cond = If->Evaluate(env); if (!cond.IsBool()) env->ThrowError("Evaluate: left of `?' must be boolean (true/false)"); return (cond.AsBool() ? Then : Else)->Evaluate(env); }
int __stdcall dimzon_avs_getvariable_s(SafeStruct* pstr, const char* name, char* result, int len) { try { pstr->err[0] = 0; try { AVSValue var = pstr->env->GetVar(name); if(var.Defined()) { if(!var.IsString()) { strncpy_s(pstr->err, ERRMSG_LEN, "AviSynthWrapper: Requested variable is not String!", _TRUNCATE); return AVS_VARWRNGTYPE; } strncpy_s(result, len, var.AsString(), len - 1); return 0; } return AVS_VARNDEFINED; } catch(AvisynthError err) { strncpy_s(pstr->err, ERRMSG_LEN, err.msg, _TRUNCATE); return AVS_GERROR; } } catch(IScriptEnvironment::NotFound) { return AVS_VARNFOUND; } }
AVSValue ExpWhileLoop::Evaluate(IScriptEnvironment* env) { AVSValue result; IScriptEnvironment2 *env2 = static_cast<IScriptEnvironment2*>(env); env2->GetVar("last", &result); AVSValue cond; do { cond = condition->Evaluate(env); if (!cond.IsBool()) env->ThrowError("while: condition must be boolean (true/false)"); if (!cond.AsBool()) break; if (body) { try { result = body->Evaluate(env); if (result.IsClip()) env->SetVar("last", result); } catch(const BreakStmtException&) { break; } } } while (true); return result; }
bool Write::DoEval( IScriptEnvironment* env) { bool keep_this_line = true; int i; AVSValue expr; AVSValue result; for (i=0; i<arrsize; i++) { expr = arglist[i].expression; if ( (linecheck==1) && (i==0)) { try { result = env->Invoke("Eval",expr); if (!result.AsBool(true)) { keep_this_line = false; break; } } catch (AvisynthError) { // env->ThrowError("Write: Can't eval linecheck expression!"); // results in KEEPING the line } } else { try { result = env->Invoke("Eval",expr); result = env->Invoke("string",result); //convert all results to a string arglist[i].string = result.AsString(EMPTY); } catch (AvisynthError error) { arglist[i].string = env->SaveString(error.msg); } } } return keep_this_line; }
AVSValue ExpVariableReference::Evaluate(IScriptEnvironment* env) { AVSValue result; IScriptEnvironment2 *env2 = static_cast<IScriptEnvironment2*>(env); // first look for a genuine variable // Don't add a cache to this one, it's a Var if (env2->GetVar(name, &result)) { return result; } else { // Swap order to match ::Call below -- Gavino Jan 2010 // next look for an argless function if (!env2->Invoke(&result, name, AVSValue(0,0))) { // finally look for a single-arg function taking implicit "last" AVSValue last; if (!env2->GetVar("last", &last) || !env2->Invoke(&result, name, last)) { env->ThrowError("I don't know what '%s' means.", name); return 0; } } } // Add cache to Bracketless call of argless function if (result.IsClip()) { // Tritical Jan 2006 return env->Invoke("Cache", result); } return result; }
void ConditionalReader::SetRange(int start_frame, int stop_frame, AVSValue v) { int i; start_frame = std::max(std::min(start_frame, vi.num_frames-1), 0); stop_frame = std::max(std::min(stop_frame, vi.num_frames-1), 0); int p; float q; bool r; switch (mode) { case MODE_INT: p = v.AsInt(); for (i = start_frame; i <= stop_frame; i++) { intVal[i] = p; } break; case MODE_FLOAT: q = (float)v.AsFloat(); for (i = start_frame; i <= stop_frame; i++) { floatVal[i] = q; } break; case MODE_BOOL: r = v.AsBool(); for (i = start_frame; i <= stop_frame; i++) { boolVal[i] = r; } break; } }
PVideoFrame __stdcall ConditionalSelect::GetFrame(int n, IScriptEnvironment* env) { AVSValue prev_last = env->GetVarDef("last"); // Store previous last AVSValue prev_current_frame = env->GetVarDef("current_frame"); // Store previous current_frame env->SetVar("last", (AVSValue)child); // Set implicit last env->SetVar("current_frame", (AVSValue)n); // Set frame to be tested by the conditional filters. AVSValue result; try { ScriptParser parser(env, expression, "[Conditional Select, Expression]"); PExpression exp = parser.Parse(); result = exp->Evaluate(env); if (!result.IsInt()) env->ThrowError("Conditional Select: Expression must return an integer!"); } catch (AvisynthError error) { env->SetVar("last", prev_last); // Restore implicit last env->SetVar("current_frame", prev_current_frame); // Restore current_frame const int num_frames = child->GetVideoInfo().num_frames; PVideoFrame dst = child->GetFrame(min(num_frames-1, n), env); env->MakeWritable(&dst); env->ApplyMessage(&dst, vi, error.msg, vi.width/W_DIVISOR, 0xa0a0a0, 0, 0); return dst; } env->SetVar("last", prev_last); // Restore implicit last env->SetVar("current_frame", prev_current_frame); // Restore current_frame const int i = result.AsInt(); PVideoFrame dst; if (i < 0 || i >= num_args) { const int num_frames = child->GetVideoInfo().num_frames; dst = child->GetFrame(min(num_frames-1, n), env); } else { const int num_frames = child_array[i]->GetVideoInfo().num_frames; dst = child_array[i]->GetFrame(min(num_frames-1, n), env); } if (show) { char text[32]; _snprintf(text, sizeof(text)-1, "Expression Result:%i\n", result.AsInt()); text[sizeof(text)-1] = '\0'; env->MakeWritable(&dst); env->ApplyMessage(&dst, vi, text, vi.width/4, 0xa0a0a0, 0, 0); } return dst; }
AVSValue __cdecl CSRIAviSynth::Create(AVSValue args, void* user_data, IScriptEnvironment* env) { const char *rname = args.ArraySize() >= 2 ? args[2].AsString() : NULL, *rver = args.ArraySize() >= 3 ? args[3].AsString() : NULL; return new CSRIAviSynth(args[0].AsClip(), env, args[1].AsString(), rname, rver); }
MVClipArray::MVClipArray(const AVSValue &vectors, int nSCD1, int nSCD2, IScriptEnvironment *env) { DebugPrintf("Constructing mvClipArray... : %i", vectors.ArraySize()); size_ = vectors.ArraySize(); pmvClips = new MVClip*[size_]; for ( int i = 0; i < size_; i++ ) pmvClips[i] = new MVClip(vectors[i].AsClip(), nSCD1, nSCD2, env); }
AVSValue ExpFunctionCall::Evaluate(IScriptEnvironment* env) { AVSValue result = Call(env); if (result.IsClip()) { return env->Invoke("Cache", result); } return result; }
AVSValue ExpNot::Evaluate(IScriptEnvironment* env) { AVSValue x = e->Evaluate(env); if (x.IsBool()) return !x.AsBool(); else { env->ThrowError("Evaluate: operand of `!' must be boolean (true/false)"); return 0; } }
AVSValue ExpAnd::Evaluate(IScriptEnvironment* env) { AVSValue x = a->Evaluate(env); if (!x.IsBool()) env->ThrowError("Evaluate: left operand of && must be boolean (true/false)"); if (!x.AsBool()) return x; AVSValue y = b->Evaluate(env); if (!y.IsBool()) env->ThrowError("Evaluate: right operand of && must be boolean (true/false)"); return y; }
AVSValue ExpNegate::Evaluate(IScriptEnvironment* env) { AVSValue x = e->Evaluate(env); if (x.IsInt()) return -x.AsInt(); else if (x.IsFloat()) return -x.AsFloat(); else { env->ThrowError("Evaluate: unary minus can only by used with numbers"); return 0; } }
AVSValue ExpMult::Evaluate(IScriptEnvironment* env) { AVSValue x = a->Evaluate(env); AVSValue y = b->Evaluate(env); if (x.IsInt() && y.IsInt()) return x.AsInt() * y.AsInt(); else if (x.IsFloat() && y.IsFloat()) return x.AsFloat() * y.AsFloat(); else { env->ThrowError("Evaluate: operands of `*' must be numeric"); return 0; } }
Echo( PClip _child, const AVSValue _clips, IScriptEnvironment* env ) : GenericVideoFilter(_child), ClipCount(_clips.ArraySize()) { clips = new PClip[ClipCount]; for (int i=0; i < ClipCount; i++) clips[i] = _clips[i].AsClip(); }
int AvxContext::OpenFile() { try { AVSValue ret = avx_library.env->Invoke("Import", scriptName); if (!ret.IsClip()) { AVXLOG_ERROR("%s", "Script did not return a clip"); return -1; } clip = ret.AsClip(); vi = clip->GetVideoInfo(); } catch (AvisynthError &e) { AVXLOG_ERROR("AvisynthError: %s", e.msg); return -1; } return 0; }
void ConditionalReader::SetFrame(int framenumber, AVSValue v) { if (framenumber < 0 || framenumber > vi.num_frames-1 ) return; switch (mode) { case MODE_INT: intVal[framenumber] = v.AsInt(); break; case MODE_FLOAT: floatVal[framenumber] = (float)v.AsFloat(); break; case MODE_BOOL: boolVal[framenumber] = v.AsBool(); break; } }
int __stdcall dimzon_avs_isfuncexists(SafeStruct* pstr, const char *func) { pstr->err[0] = 0; try { AVSValue var = pstr->env->FunctionExists(func); if (var.IsBool()) { return (var.AsBool()) ? 0 : AVS_VARNFOUND; } return AVS_VARWRNGTYPE; } catch(AvisynthError err) { strncpy_s(pstr->err, ERRMSG_LEN, err.msg, _TRUNCATE); return AVS_GERROR; } }
static AVSValue __cdecl create_iscombed(AVSValue args, void*, ise_t* env) { enum { CLIP, CTHRESH, MTHRESH, MI, BLOCKX, BLOCKY, METRIC, OPT }; CombMask* cm = nullptr; try { AVSValue cf = env->GetVar("current_frame"); validate(!cf.IsInt(), "This filter can only be used within ConditionalFilter."); int n = cf.AsInt(); PClip clip = args[CLIP].AsClip(); int metric = args[METRIC].AsInt(0); int cth = args[CTHRESH].AsInt(metric == 0 ? 6 : 10); int mth = args[MTHRESH].AsInt(9); int mi = args[MI].AsInt(80); int blockx = args[BLOCKX].AsInt(16); int blocky = args[BLOCKY].AsInt(16); bool is_avsplus = env->FunctionExists("SetFilterMTMode"); arch_t arch = get_arch(args[OPT].AsInt(-1), is_avsplus); validate(mi < 0 || mi > 128, "MI must be between 0 and 128."); validate(blockx != 8 && blockx != 16 && blockx != 32, "blockx must be set to 8, 16 or 32."); validate(blocky != 8 && blocky != 16 && blocky != 32, "blocky must be set to 8, 16 or 32."); cm = new CombMask(clip, cth, mth, false, arch, false, metric, is_avsplus); bool is_combed = (get_check_combed(arch))( cm->GetFrame(n, env), mi, blockx, blocky, is_avsplus, env); delete cm; return AVSValue(is_combed); } catch (std::runtime_error& e) { if (cm) delete cm; env->ThrowError("IsCombed: %s", e.what()); } return 0; }
void ConditionalReader::SetFrame(int framenumber, AVSValue v) { if ((framenumber+offset) < 0 || (framenumber+offset) > vi.num_frames-1 ) return; switch (mode) { case MODE_INT: intVal[framenumber+offset] = v.AsInt(); break; case MODE_FLOAT: floatVal[framenumber+offset] = v.AsFloatf(); break; case MODE_BOOL: boolVal[framenumber+offset] = v.AsBool(); break; case MODE_STRING: stringVal[framenumber+offset] = v.AsString(""); break; } }
AVSValue ExpBlockConditional::Evaluate(IScriptEnvironment* env) { AVSValue result; IScriptEnvironment2 *env2 = static_cast<IScriptEnvironment2*>(env); env2->GetVar("last", &result); AVSValue cond = If->Evaluate(env); if (!cond.IsBool()) env->ThrowError("if: condition must be boolean (true/false)"); if (cond.AsBool()) { if (Then) // note: "Then" can also be NULL if its block is empty result = Then->Evaluate(env); } else if (Else) // note: "Else" can also be NULL if its block is empty result = Else->Evaluate(env); if (result.IsClip()) env->SetVar("last", result); return result; }
/// @brief Read from environment /// @param _clip /// void AvisynthAudioProvider::LoadFromClip(AVSValue _clip) { AVSValue script; // Check if it has audio VideoInfo vi = _clip.AsClip()->GetVideoInfo(); if (!vi.HasAudio()) throw agi::AudioDataNotFoundError("No audio found.", 0); IScriptEnvironment *env = avs_wrapper.GetEnv(); // Convert to one channel char buffer[1024]; strcpy(buffer,lagi_wxString(OPT_GET("Audio/Downmixer")->GetString()).mb_str(csConvLocal)); script = env->Invoke(buffer, _clip); // Convert to 16 bits per sample script = env->Invoke("ConvertAudioTo16bit", script); vi = script.AsClip()->GetVideoInfo(); // Convert sample rate int setsample = OPT_GET("Provider/Audio/AVS/Sample Rate")->GetInt(); if (vi.SamplesPerSecond() < 32000) setsample = 44100; if (setsample != 0) { AVSValue args[2] = { script, setsample }; script = env->Invoke("ResampleAudio", AVSValue(args,2)); } // Set clip PClip tempclip = script.AsClip(); vi = tempclip->GetVideoInfo(); // Read properties channels = vi.AudioChannels(); num_samples = vi.num_audio_samples; sample_rate = vi.SamplesPerSecond(); bytes_per_sample = vi.BytesPerAudioSample(); float_samples = false; clip = tempclip; }
void ConditionalReader::SetRange(int start_frame, int stop_frame, AVSValue v) { int i; start_frame = max(start_frame+offset, 0); stop_frame = min(stop_frame+offset, vi.num_frames-1); int p; float q; bool r; const char* s; switch (mode) { case MODE_INT: p = v.AsInt(); for (i = start_frame; i <= stop_frame; i++) { intVal[i] = p; } break; case MODE_FLOAT: q = v.AsFloatf(); for (i = start_frame; i <= stop_frame; i++) { floatVal[i] = q; } break; case MODE_BOOL: r = v.AsBool(); for (i = start_frame; i <= stop_frame; i++) { boolVal[i] = r; } break; case MODE_STRING: s = v.AsString(""); for (i = start_frame; i <= stop_frame; i++) { stringVal[i] = s; } break; } }
AVSValue ExpMod::Evaluate(IScriptEnvironment* env) { AVSValue x = a->Evaluate(env); AVSValue y = b->Evaluate(env); if (x.IsInt() && y.IsInt()) { if (y.AsInt() == 0) env->ThrowError("Evaluate: division by zero"); return x.AsInt() % y.AsInt(); } else { env->ThrowError("Evaluate: operands of `%%' must be integers"); return 0; } }
AVSValue ExpDoublePlus::Evaluate(IScriptEnvironment* env) { AVSValue x = a->Evaluate(env); AVSValue y = b->Evaluate(env); if (x.IsClip() && y.IsClip()) return new_Splice(x.AsClip(), y.AsClip(), true, env); // AlignedSplice else { env->ThrowError("Evaluate: operands of `++' must be clips"); return 0; } }
AVSValue LoadPlugin(AVSValue args, void* user_data, IScriptEnvironment* env) { bool quiet = (user_data != 0); args = args[0]; const char* result=0; for (int i=0; i<args.ArraySize(); ++i) { HMODULE plugin; const char* plugin_name = args[i].AsString(); if (MyLoadLibrary(plugin_name, &plugin, quiet, env)) { typedef const char* (__stdcall *AvisynthPluginInitFunc)(IScriptEnvironment* env); AvisynthPluginInitFunc AvisynthPluginInit = (AvisynthPluginInitFunc)dlsym(plugin, "AvisynthPluginInit2"); if (!AvisynthPluginInit) { AvisynthPluginInit = (AvisynthPluginInitFunc)dlsym(plugin, "_AvisynthPluginInit2@4"); /* if (!AvisynthPluginInit) { // Attempt C-plugin AvisynthPluginInit = (AvisynthPluginInitFunc)dlsym(plugin, "avisynth_c_plugin_init"); if (AvisynthPluginInit) { dlclose(plugin); return env->Invoke("LoadCPlugin", args); } } */ if (!AvisynthPluginInit) { // Older version dlclose(plugin); if (quiet) { // remove the last handle from the list HMODULE* loaded_plugins = (HMODULE*)env->GetVar("$Plugins$").AsString(); int j=0; while (loaded_plugins[j+1]) j++; loaded_plugins[j] = 0; } else { env->ThrowError("Plugin %s is not an AviSynth 2.5 plugin.",plugin_name); } } else { result = AvisynthPluginInit(env); } } else { result = AvisynthPluginInit(env); } } } if (loadplugin_prefix) free((void*)loadplugin_prefix); // Tritical May 2005 loadplugin_prefix = 0; return result ? AVSValue(result) : AVSValue(); }
Write::Write (PClip _child, const char* _filename, AVSValue args, int _linecheck, bool _append, bool _flush, IScriptEnvironment* env): GenericVideoFilter(_child), linecheck(_linecheck), flush(_flush), append(_append), arglist(0) { _fullpath(filename, _filename, _MAX_PATH); fout = fopen(filename, append ? AplusT : WplusT); //append or purge file if (!fout) env->ThrowError("Write: File '%s' cannot be opened.", filename); if (flush) fclose(fout); //will be reopened in FileOut arrsize = args.ArraySize(); arglist = new exp_res[arrsize]; for (int i=0; i<arrsize; i++) { arglist[i].expression = args[i].AsString(EMPTY); arglist[i].string = EMPTY; } if (linecheck == -1) { //write at start AVSValue prev_last = env->GetVarDef("last"); // Store previous last AVSValue prev_current_frame = env->GetVarDef("current_frame"); // Store previous current_frame env->SetVar("last", (AVSValue)child); // Set implicit last env->SetVar("current_frame", -1); Write::DoEval(env); Write::FileOut(env, AplusT); env->SetVar("last", prev_last); // Restore implicit last env->SetVar("current_frame", prev_current_frame); // Restore current_frame } if (linecheck == -2) { //write at end, evaluate right now AVSValue prev_last = env->GetVarDef("last"); // Store previous last AVSValue prev_current_frame = env->GetVarDef("current_frame"); // Store previous current_frame env->SetVar("last", (AVSValue)child); // Set implicit last env->SetVar("current_frame", -2); Write::DoEval(env); env->SetVar("last", prev_last); // Restore implicit last env->SetVar("current_frame", prev_current_frame); // Restore current_frame } }
MVMultiExtract::MVMultiExtract(PClip _child, AVSValue _indicies, IScriptEnvironment* env) : GenericVideoFilter(_child), NumIndices(_indicies.ArraySize()) { if ( NumIndices<1 || NumIndices>=vi.height) env->ThrowError("MVMultiExtract : Number of indicies must be between 0 and number of elements in array minus 1"); // extract the indicies for(int IndexNum=0; IndexNum<NumIndices; ++IndexNum) { Index[IndexNum]=_indicies[IndexNum].AsInt(); } // check indicies for(int IndexNum=0; IndexNum<NumIndices; ++IndexNum) { if (Index[IndexNum]<0 || Index[IndexNum]>=vi.height) env->ThrowError("MVMultiExtract: Index must be between 0 and number of elements in array minus 1"); if (IndexNum>0) { if (Index[IndexNum]<=Index[IndexNum-1]) env->ThrowError("MVMultiExtract: Indicies must be in ascending order"); } } // adjust pointer to the appropriate index MVAnalysisData *pAnalyseFilterArray = reinterpret_cast<MVAnalysisData*>(vi.nchannels); if (vi.nchannels >= 0 && vi.nchannels < 9) // seems some normal clip instead of vectors env->ThrowError("MVMultiExtract: invalid vectors stream"); // create array to hold indicies pAnalysisData=new MVAnalysisData[NumIndices]; // update video information for number of rows and pointer to analysis data vi.height=NumIndices; vi.nchannels = reinterpret_cast<int>(pAnalysisData); // copy analysis data rows for(int IndexNum=0; IndexNum<NumIndices; ++IndexNum) { memcpy(reinterpret_cast<void*>(&pAnalysisData[IndexNum]), reinterpret_cast<void const*>(&pAnalyseFilterArray[Index[IndexNum]]), sizeof(MVAnalysisData)); } }