mirror of https://github.com/odrling/Aegisub
Support a free-form string argument passed from Avisynth to the Lua script.
Originally committed to SVN as r1476.
This commit is contained in:
parent
0211960d8e
commit
0a584a7cd4
|
@ -40,7 +40,7 @@ private:
|
||||||
double spf; // seconds per frame - for frame/timestamp conversion
|
double spf; // seconds per frame - for frame/timestamp conversion
|
||||||
|
|
||||||
public:
|
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)
|
: GenericVideoFilter(_child)
|
||||||
{
|
{
|
||||||
switch (vi.pixel_type) {
|
switch (vi.pixel_type) {
|
||||||
|
@ -53,7 +53,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
script = new OverLuaScript(file);
|
script = new OverLuaScript(file, datastring);
|
||||||
spf = (double)vi.fps_denominator / (double)vi.fps_numerator;
|
spf = (double)vi.fps_denominator / (double)vi.fps_numerator;
|
||||||
}
|
}
|
||||||
catch (const char *e) {
|
catch (const char *e) {
|
||||||
|
@ -112,14 +112,14 @@ public:
|
||||||
|
|
||||||
static AVSValue __cdecl Create(AVSValue args, void* user_data, IScriptEnvironment* env)
|
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)
|
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";
|
return "OverLua";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,27 @@ OverLua provides a Lua 5.1 runtime environment with access to all
|
||||||
standard libraries.
|
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
|
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.
|
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
|
The frame object
|
||||||
----------------
|
----------------
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ struct FileScriptReader {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
OverLuaScript::OverLuaScript(const char *filename)
|
OverLuaScript::OverLuaScript(const char *filename, const char *datastring)
|
||||||
{
|
{
|
||||||
FileScriptReader reader;
|
FileScriptReader reader;
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
@ -61,7 +61,7 @@ OverLuaScript::OverLuaScript(const char *filename)
|
||||||
reader.file = fopen(filename, "r");
|
reader.file = fopen(filename, "r");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Create(reader, filename);
|
Create(reader, filename, datastring);
|
||||||
|
|
||||||
fclose(reader.file);
|
fclose(reader.file);
|
||||||
}
|
}
|
||||||
|
@ -84,36 +84,20 @@ int OverLuaScript::lua_debug_print(lua_State *L)
|
||||||
{
|
{
|
||||||
const char *str = luaL_checkstring(L, 1);
|
const char *str = luaL_checkstring(L, 1);
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
OutputDebugStringA(str);
|
OutputDebugStringA(str); // ought to be W version but conversion is such a hassle
|
||||||
#else
|
#else
|
||||||
printf(str);
|
printf(str);
|
||||||
#endif
|
#endif
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
OverLuaScript::OverLuaScript(const void *data, size_t length)
|
OverLuaScript::OverLuaScript(const void *data, size_t length, const char *datastring)
|
||||||
{
|
{
|
||||||
MemScriptReader reader;
|
MemScriptReader reader;
|
||||||
reader.memdata = data;
|
reader.memdata = data;
|
||||||
reader.memdatasize = length;
|
reader.memdatasize = length;
|
||||||
|
|
||||||
Create(reader, "Memory script");
|
Create(reader, "Memory script", datastring);
|
||||||
|
|
||||||
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";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
OverLuaScript::~OverLuaScript()
|
OverLuaScript::~OverLuaScript()
|
||||||
|
@ -123,18 +107,12 @@ OverLuaScript::~OverLuaScript()
|
||||||
|
|
||||||
void OverLuaScript::RenderFrameRGB(OverLuaFrameAggregate &frame, double time)
|
void OverLuaScript::RenderFrameRGB(OverLuaFrameAggregate &frame, double time)
|
||||||
{
|
{
|
||||||
OutputDebugStringW(L"RenderFrameRGB: get frame func\n");
|
|
||||||
lua_getglobal(L, "render_frame");
|
lua_getglobal(L, "render_frame");
|
||||||
OutputDebugStringW(L"RenderFrameRGB: CreateLuaObject\n");
|
|
||||||
frame.CreateLuaObject(L);
|
frame.CreateLuaObject(L);
|
||||||
lua_pushnumber(L, time);
|
lua_pushnumber(L, time);
|
||||||
OutputDebugStringW(L"RenderFrameRGB: call\n");
|
|
||||||
if (lua_pcall(L, 2, 0, 0)) {
|
if (lua_pcall(L, 2, 0, 0)) {
|
||||||
const char *err = lua_tostring(L, -1);
|
const char *err = lua_tostring(L, -1);
|
||||||
//MessageBoxA(0, err, "OverLua", MB_ICONERROR);
|
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
OutputDebugStringW(L"RenderFrameRGB: garbage collect\n");
|
|
||||||
lua_gc(L, LUA_GCCOLLECT, 0);
|
lua_gc(L, LUA_GCCOLLECT, 0);
|
||||||
OutputDebugStringW(L"RenderFrameRGB: done rendering frame\n");
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,7 @@ private:
|
||||||
OverLuaScript() { }
|
OverLuaScript() { }
|
||||||
|
|
||||||
template <class ScriptReaderClass>
|
template <class ScriptReaderClass>
|
||||||
void Create(ScriptReaderClass &reader, const char *chunkname)
|
void Create(ScriptReaderClass &reader, const char *chunkname, const char *datastring)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
|
@ -62,6 +62,12 @@ private:
|
||||||
// Debug print
|
// Debug print
|
||||||
lua_pushcclosure(L, lua_debug_print, 0);
|
lua_pushcclosure(L, lua_debug_print, 0);
|
||||||
lua_setglobal(L, "dprint");
|
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);
|
err = lua_load(L, reader.reader, &reader, chunkname);
|
||||||
|
|
||||||
|
@ -79,8 +85,8 @@ private:
|
||||||
static int lua_debug_print(lua_State *L);
|
static int lua_debug_print(lua_State *L);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
OverLuaScript(const char *filename);
|
OverLuaScript(const char *filename, const char *_datastring = 0);
|
||||||
OverLuaScript(const void *data, size_t length);
|
OverLuaScript(const void *data, size_t length, const char *_datastring = 0);
|
||||||
virtual ~OverLuaScript();
|
virtual ~OverLuaScript();
|
||||||
|
|
||||||
void RenderFrameRGB(OverLuaFrameAggregate &frame, double time);
|
void RenderFrameRGB(OverLuaFrameAggregate &frame, double time);
|
||||||
|
|
Loading…
Reference in New Issue