Support a free-form string argument passed from Avisynth to the Lua script.

Originally committed to SVN as r1476.
This commit is contained in:
Niels Martin Hansen 2007-08-11 20:40:37 +00:00
parent 0211960d8e
commit 0a584a7cd4
4 changed files with 44 additions and 34 deletions

View file

@ -40,7 +40,7 @@ private:
double spf; // seconds per frame - for frame/timestamp conversion
public:
OverLuaAvisynth(PClip _child, IScriptEnvironment *env, const char *file)
OverLuaAvisynth(PClip _child, IScriptEnvironment *env, const char *file, const char *datastring, const char *vfrfile)
: GenericVideoFilter(_child)
{
switch (vi.pixel_type) {
@ -53,7 +53,7 @@ public:
}
try {
script = new OverLuaScript(file);
script = new OverLuaScript(file, datastring);
spf = (double)vi.fps_denominator / (double)vi.fps_numerator;
}
catch (const char *e) {
@ -112,14 +112,14 @@ public:
static AVSValue __cdecl Create(AVSValue args, void* user_data, IScriptEnvironment* env)
{
return new OverLuaAvisynth(args[0].AsClip(), env, args[1].AsString());
return new OverLuaAvisynth(args[0].AsClip(), env, args[1].AsString(), args[2].AsString(0), args[3].AsString(0));
}
};
extern "C" __declspec(dllexport) const char* __stdcall AvisynthPluginInit2(IScriptEnvironment* env)
{
env->AddFunction("OverLua", "cs", OverLuaAvisynth::Create, 0);
env->AddFunction("OverLua", "cs[data]s[vfr]s", OverLuaAvisynth::Create, 0);
return "OverLua";
}

View file

@ -2,6 +2,27 @@ OverLua provides a Lua 5.1 runtime environment with access to all
standard libraries.
The Avisynth filter
-------------------
The Avisynth filter version of OverLua exports one function:
OverLua(clip c, string scriptfile, string "datastring", string "vfrfile")
The clip c argument is self-explanatory, the clip to render on.
The scripfile is the path to the Lua script you want to load and run.
datastring is a freeform string to pass to the Lua script. This will often
be the path to a file with additional data for the script, such as timed
subtitles/karaoke.
vfrfile is the path to a timecode file Matroska format 1 or 2. If supplied,
it will be used to translate frame numbers to timestamps instead of relying
on the frame rate provided by Avisynth.
VFR support is not implemented yet.
API the Lua script must implement
---------------------------------
@ -28,6 +49,11 @@ The render_frame function should not assume that frames are requested in
any specific order.
If a "datastring" argument is supplied to the OverLua Avisynth function, the
argument value will be available through the global variable
"overlua_datastring" in the Lua script environment.
The frame object
----------------

View file

@ -49,7 +49,7 @@ struct FileScriptReader {
}
};
OverLuaScript::OverLuaScript(const char *filename)
OverLuaScript::OverLuaScript(const char *filename, const char *datastring)
{
FileScriptReader reader;
#ifdef WIN32
@ -61,7 +61,7 @@ OverLuaScript::OverLuaScript(const char *filename)
reader.file = fopen(filename, "r");
#endif
Create(reader, filename);
Create(reader, filename, datastring);
fclose(reader.file);
}
@ -84,36 +84,20 @@ int OverLuaScript::lua_debug_print(lua_State *L)
{
const char *str = luaL_checkstring(L, 1);
#ifdef WIN32
OutputDebugStringA(str);
OutputDebugStringA(str); // ought to be W version but conversion is such a hassle
#else
printf(str);
#endif
return 0;
}
OverLuaScript::OverLuaScript(const void *data, size_t length)
OverLuaScript::OverLuaScript(const void *data, size_t length, const char *datastring)
{
MemScriptReader reader;
reader.memdata = data;
reader.memdatasize = length;
Create(reader, "Memory script");
int err;
L = luaL_newstate();
err = lua_load(L, reader.reader, &reader, "Memory script");
// todo: better error handling
if (err == LUA_ERRSYNTAX) throw "Syntax error";
if (err = LUA_ERRMEM) throw "Memory error";
err = lua_pcall(L, 0, 0, 0);
if (err == LUA_ERRRUN) throw "Runtime error";
if (err == LUA_ERRMEM) throw "Memory error";
if (err == LUA_ERRERR) throw "Error-handler error";
Create(reader, "Memory script", datastring);
}
OverLuaScript::~OverLuaScript()
@ -123,18 +107,12 @@ OverLuaScript::~OverLuaScript()
void OverLuaScript::RenderFrameRGB(OverLuaFrameAggregate &frame, double time)
{
OutputDebugStringW(L"RenderFrameRGB: get frame func\n");
lua_getglobal(L, "render_frame");
OutputDebugStringW(L"RenderFrameRGB: CreateLuaObject\n");
frame.CreateLuaObject(L);
lua_pushnumber(L, time);
OutputDebugStringW(L"RenderFrameRGB: call\n");
if (lua_pcall(L, 2, 0, 0)) {
const char *err = lua_tostring(L, -1);
//MessageBoxA(0, err, "OverLua", MB_ICONERROR);
throw err;
}
OutputDebugStringW(L"RenderFrameRGB: garbage collect\n");
lua_gc(L, LUA_GCCOLLECT, 0);
OutputDebugStringW(L"RenderFrameRGB: done rendering frame\n");
}

View file

@ -47,7 +47,7 @@ private:
OverLuaScript() { }
template <class ScriptReaderClass>
void Create(ScriptReaderClass &reader, const char *chunkname)
void Create(ScriptReaderClass &reader, const char *chunkname, const char *datastring)
{
int err;
@ -62,6 +62,12 @@ private:
// Debug print
lua_pushcclosure(L, lua_debug_print, 0);
lua_setglobal(L, "dprint");
// Datastring
if (datastring)
lua_pushstring(L, datastring);
else
lua_pushnil(L);
lua_setglobal(L, "overlua_datastring");
err = lua_load(L, reader.reader, &reader, chunkname);
@ -79,8 +85,8 @@ private:
static int lua_debug_print(lua_State *L);
public:
OverLuaScript(const char *filename);
OverLuaScript(const void *data, size_t length);
OverLuaScript(const char *filename, const char *_datastring = 0);
OverLuaScript(const void *data, size_t length, const char *_datastring = 0);
virtual ~OverLuaScript();
void RenderFrameRGB(OverLuaFrameAggregate &frame, double time);