-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavisynth.cpp
138 lines (114 loc) · 3.74 KB
/
avisynth.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Avisynth interface for OverLua
*
Copyright 2007 Niels Martin Hansen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Contact:
E-mail: <jiifurusu@gmail.com>
IRC: jfs in #aegisub on irc.rizon.net
*/
// Must be included before <windows.h>, otherwise some macros from <windows.h> get in the way
#include "image.h"
#include <windows.h>
#include <string.h>
#include <memory.h>
#include "avisynth.h"
#include "overlua.h"
#include "vfr.h"
// Lots of code lifted from the CSRI avisynth.cpp
class OverLuaAvisynth : public GenericVideoFilter {
private:
OverLuaScript *script;
double spf; // seconds per frame - for frame/timestamp conversion
VFRTranslator *vfr;
public:
OverLuaAvisynth(PClip _child, IScriptEnvironment *env, const char *file, const char *datastring, const char *vfrfile)
: GenericVideoFilter(_child)
{
switch (vi.pixel_type) {
case VideoInfo::CS_BGR24:
case VideoInfo::CS_BGR32:
// safe
break;
default:
env->ThrowError("OverLua: Unsupported pixel format, only RGB formats supported");
}
try {
script = new OverLuaScript(file, datastring);
spf = (double)vi.fps_denominator / (double)vi.fps_numerator;
if (vfrfile)
vfr = GetVFRTranslator(vfrfile);
else
vfr = 0;
}
catch (const char *e) {
env->ThrowError(e);
}
catch (...) {
env->ThrowError("Unknown exception in OverLua");
}
}
~OverLuaAvisynth()
{
if (vfr)
delete vfr;
delete script;
}
PVideoFrame __stdcall GetFrame(int n, IScriptEnvironment *env)
{
OutputDebugStringW(L"Entering OverLua GetFrame\n");
PVideoFrame avsframe = child->GetFrame(n, env);
env->MakeWritable(&avsframe);
double frametime = n * spf;
if (vfr)
frametime = vfr->TimeStampFromFrameNumber(n);
ptrdiff_t stride = avsframe->GetPitch();
unsigned char *plane = avsframe->GetWritePtr();
plane += (vi.height - 1) * stride;
stride = -stride;
try {
switch (vi.pixel_type) {
case VideoInfo::CS_BGR24: {
ImageBGR *frame = new ImageBGR(vi.width, vi.height, stride, plane);
script->RenderFrameRGB(*frame, frametime);
} break;
case VideoInfo::CS_BGR32: {
ImageBGRX *frame = new ImageBGRX(vi.width, vi.height, stride, plane);
script->RenderFrameRGB(*frame, frametime);
} break;
}
}
catch (const char *e) {
wchar_t *ew = new wchar_t[2048];
MultiByteToWideChar(CP_UTF8, 0, e, -1, ew, 2048);
MessageBoxW(0, ew, L"OverLua execution error", MB_ICONERROR);
delete[] ew;
env->ThrowError(e);
}
catch (...) {
MessageBoxW(0, L"Unknown error", L"OverLua execution error", MB_ICONERROR);
env->ThrowError("OverLua: unknown execution error");
}
OutputDebugStringW(L"Leaving OverLua GetFrame\n");
return avsframe;
}
static AVSValue __cdecl Create(AVSValue args, void* user_data, IScriptEnvironment* env)
{
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[data]s[vfr]s", OverLuaAvisynth::Create, 0);
return "OverLua";
}