forked from AviSynth/AviSynthPlus
/
ScriptEnvironmentTLS.h
424 lines (350 loc) · 10.4 KB
/
ScriptEnvironmentTLS.h
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#ifndef _SCRIPTENVIRONMENTTLS_H
#define _SCRIPTENVIRONMENTTLS_H
#include <avisynth.h>
#include <cstdarg>
#include "vartable.h"
#include "ThreadPool.h"
#include "BufferPool.h"
#include "InternalEnvironment.h"
class ScriptEnvironmentTLS : public InternalEnvironment
{
private:
InternalEnvironment *core;
const size_t thread_id;
VarTable* global_var_table;
// PF 161223 why do we need thread-local global variables?
// comment remains here until it gets cleared, anyway, I make it of no use
VarTable* var_table;
BufferPool BufferPool;
public:
ScriptEnvironmentTLS(size_t _thread_id) :
core(NULL),
thread_id(_thread_id),
global_var_table(NULL),
var_table(NULL),
BufferPool(this)
{
global_var_table = new VarTable(0, 0);
var_table = new VarTable(0, global_var_table);
}
~ScriptEnvironmentTLS()
{
while (var_table)
PopContext();
while (global_var_table)
PopContextGlobal();
}
void Specialize(InternalEnvironment* _core)
{
core = _core;
}
/* ---------------------------------------------------------------------------------
* T L S
* ---------------------------------------------------------------------------------
*/
AVSValue __stdcall GetVar(const char* name)
{
AVSValue val;
if (var_table->Get(name, &val))
return val;
else
return core->GetVar(name);
}
bool __stdcall SetVar(const char* name, const AVSValue& val)
{
return var_table->Set(name, val);
}
bool __stdcall SetGlobalVar(const char* name, const AVSValue& val)
{
// return global_var_table->Set(name, val);
return core->SetGlobalVar(name, val);
// PF 161223 use real global table, runtime scripts can write globals from different threads
// so we don't use the TLS global_var_table
}
void __stdcall PushContext(int level=0)
{
var_table = new VarTable(var_table, global_var_table);
}
void __stdcall PopContext()
{
var_table = var_table->Pop();
}
void __stdcall PopContextGlobal()
{
global_var_table = global_var_table->Pop();
}
bool __stdcall GetVar(const char* name, AVSValue *val) const
{
if (!var_table->Get(name, val))
return core->GetVar(name, val);
return true;
}
AVSValue __stdcall GetVarDef(const char* name, const AVSValue& def)
{
AVSValue val;
if (this->GetVar(name, &val))
return val;
else
return def;
}
bool __stdcall GetVar(const char* name, bool def) const
{
AVSValue val;
if (this->GetVar(name, &val))
return val.AsBool(def);
else
return def;
}
int __stdcall GetVar(const char* name, int def) const
{
AVSValue val;
if (this->GetVar(name, &val))
return val.AsInt(def);
else
return def;
}
double __stdcall GetVar(const char* name, double def) const
{
AVSValue val;
if (this->GetVar(name, &val))
return val.AsDblDef(def);
else
return def;
}
const char* __stdcall GetVar(const char* name, const char* def) const
{
AVSValue val;
if (this->GetVar(name, &val))
return val.AsString(def);
else
return def;
}
void* __stdcall Allocate(size_t nBytes, size_t alignment, AvsAllocType type)
{
if ((type != AVS_NORMAL_ALLOC) && (type != AVS_POOLED_ALLOC))
return NULL;
return BufferPool.Allocate(nBytes, alignment, type == AVS_POOLED_ALLOC);
}
void __stdcall Free(void* ptr)
{
BufferPool.Free(ptr);
}
/* ---------------------------------------------------------------------------------
* S T U B S
* ---------------------------------------------------------------------------------
*/
bool __stdcall InternalFunctionExists(const char* name)
{
return core->InternalFunctionExists(name);
}
void __stdcall AdjustMemoryConsumption(size_t amount, bool minus)
{
core->AdjustMemoryConsumption(amount, minus);
}
void __stdcall CheckVersion(int version)
{
core->CheckVersion(version);
}
int __stdcall GetCPUFlags()
{
return core->GetCPUFlags();
}
char* __stdcall SaveString(const char* s, int length = -1)
{
return core->SaveString(s, length);
}
char* __stdcall Sprintf(const char* fmt, ...)
{
va_list val;
va_start(val, fmt);
// do not call core->Sprintf, because cannot pass ... further
char* result = core->VSprintf(fmt, val);
va_end(val);
return result;
}
char* __stdcall VSprintf(const char* fmt, void* val)
{
return core->VSprintf(fmt, val);
}
void __stdcall ThrowError(const char* fmt, ...)
{
va_list val;
va_start(val, fmt);
core->VThrowError(fmt, val);
va_end(val);
}
virtual void __stdcall VThrowError(const char* fmt, va_list va)
{
core->VThrowError(fmt, va);
}
virtual PVideoFrame __stdcall SubframePlanarA(PVideoFrame src, int rel_offset, int new_pitch, int new_row_size, int new_height, int rel_offsetU, int rel_offsetV, int new_pitchUV, int rel_offsetA)
{
return core->SubframePlanarA(src, rel_offset, new_pitch, new_row_size, new_height, rel_offsetU, rel_offsetV, new_pitchUV, rel_offsetA);
}
void __stdcall AddFunction(const char* name, const char* params, ApplyFunc apply, void* user_data=0)
{
core->AddFunction(name, params, apply, user_data);
}
bool __stdcall FunctionExists(const char* name)
{
return core->FunctionExists(name);
}
AVSValue __stdcall Invoke(const char* name, const AVSValue args, const char* const* arg_names=0)
{
return core->Invoke(name, args, arg_names);
}
PVideoFrame __stdcall NewVideoFrame(const VideoInfo& vi, int align)
{
return core->NewVideoFrame(vi, align);
}
bool __stdcall MakeWritable(PVideoFrame* pvf)
{
return core->MakeWritable(pvf);
}
void __stdcall BitBlt(BYTE* dstp, int dst_pitch, const BYTE* srcp, int src_pitch, int row_size, int height)
{
core->BitBlt(dstp, dst_pitch, srcp, src_pitch, row_size, height);
}
void __stdcall AtExit(IScriptEnvironment::ShutdownFunc function, void* user_data)
{
core->AtExit(function, user_data);
}
PVideoFrame __stdcall Subframe(PVideoFrame src, int rel_offset, int new_pitch, int new_row_size, int new_height)
{
return core->Subframe(src, rel_offset, new_pitch, new_row_size, new_height);
}
int __stdcall SetMemoryMax(int mem)
{
return core->SetMemoryMax(mem);
}
int __stdcall SetWorkingDir(const char * newdir)
{
return core->SetWorkingDir(newdir);
}
void* __stdcall ManageCache(int key, void* data)
{
return core->ManageCache(key, data);
}
bool __stdcall PlanarChromaAlignment(IScriptEnvironment::PlanarChromaAlignmentMode key)
{
return core->PlanarChromaAlignment(key);
}
PVideoFrame __stdcall SubframePlanar(PVideoFrame src, int rel_offset, int new_pitch, int new_row_size, int new_height, int rel_offsetU, int rel_offsetV, int new_pitchUV)
{
return core->SubframePlanar(src, rel_offset, new_pitch, new_row_size, new_height, rel_offsetU, rel_offsetV, new_pitchUV);
}
void __stdcall DeleteScriptEnvironment()
{
core->ThrowError("Cannot delete environment from a TLS proxy.");
}
void __stdcall ApplyMessage(PVideoFrame* frame, const VideoInfo& vi, const char* message, int size, int textcolor, int halocolor, int bgcolor)
{
core->ApplyMessage(frame, vi, message, size, textcolor, halocolor, bgcolor);
}
const AVS_Linkage* const __stdcall GetAVSLinkage()
{
return core->GetAVSLinkage();
}
/* IScriptEnvironment2 */
virtual bool __stdcall LoadPlugin(const char* filePath, bool throwOnError, AVSValue *result)
{
return core->LoadPlugin(filePath, throwOnError, result);
}
virtual void __stdcall AddAutoloadDir(const char* dirPath, bool toFront)
{
core->AddAutoloadDir(dirPath, toFront);
}
virtual void __stdcall ClearAutoloadDirs()
{
core->ClearAutoloadDirs();
}
virtual void __stdcall AutoloadPlugins()
{
core->AutoloadPlugins();
}
virtual void __stdcall AddFunction(const char* name, const char* params, ApplyFunc apply, void* user_data, const char *exportVar)
{
core->AddFunction(name, params, apply, user_data, exportVar);
}
virtual int __stdcall IncrImportDepth()
{
return core->IncrImportDepth();
}
virtual int __stdcall DecrImportDepth()
{
return core->DecrImportDepth();
}
virtual bool __stdcall Invoke(AVSValue *result, const char* name, const AVSValue& args, const char* const* arg_names=0)
{
return core->Invoke(result, name, args, arg_names=0);
}
size_t __stdcall GetProperty(AvsEnvProperty prop)
{
switch(prop)
{
case AEP_THREAD_ID:
return thread_id;
default:
return core->GetProperty(prop);
}
}
virtual void __stdcall SetFilterMTMode(const char* filter, MtMode mode, bool force)
{
core->SetFilterMTMode(filter, mode, force);
}
virtual MtMode __stdcall GetFilterMTMode(const AVSFunction* filter, bool* is_forced) const
{
return core->GetFilterMTMode(filter, is_forced);
}
bool __stdcall FilterHasMtMode(const AVSFunction* filter) const
{
return core->FilterHasMtMode(filter);
}
virtual IJobCompletion* __stdcall NewCompletion(size_t capacity)
{
return core->NewCompletion(capacity);
}
virtual void __stdcall ParallelJob(ThreadWorkerFuncPtr jobFunc, void* jobData, IJobCompletion* completion)
{
core->ParallelJob(jobFunc, jobData, completion);
}
virtual void __stdcall SetPrefetcher(Prefetcher *p)
{
core->SetPrefetcher(p);
}
virtual ClipDataStore* __stdcall ClipData(IClip *clip)
{
return core->ClipData(clip);
}
virtual MtMode __stdcall GetDefaultMtMode() const
{
return core->GetDefaultMtMode();
}
virtual void __stdcall SetLogParams(const char *target, int level)
{
core->SetLogParams(target, level);
}
virtual void __stdcall LogMsg(int level, const char* fmt, ...)
{
va_list val;
va_start(val, fmt);
core->LogMsg_valist(level, fmt, val);
va_end(val);
}
virtual void __stdcall LogMsg_valist(int level, const char* fmt, va_list va)
{
core->LogMsg_valist(level, fmt, va);
}
virtual void __stdcall LogMsgOnce(const OneTimeLogTicket &ticket, int level, const char* fmt, ...)
{
va_list val;
va_start(val, fmt);
core->LogMsgOnce_valist(ticket, level, fmt, val);
va_end(val);
}
virtual void __stdcall LogMsgOnce_valist(const OneTimeLogTicket &ticket, int level, const char* fmt, va_list va)
{
core->LogMsgOnce_valist(ticket, level, fmt, va);
}
};
#endif // _SCRIPTENVIRONMENTTLS_H