Debian Codegraph @

src/avisynth/avisynth.cpp

raw symbols SCIP
1 // Copyright (c) 2007-2017 Fredrik Mellbin
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20
21 #include <string>
22 #include "ffms.h"
23 #include "avssources.h"
24 #include "../core/utils.h"
25
26 #ifdef _WIN32
27 #define AVS_EXPORT __declspec(dllexport)
28 #else
29 #define AVS_EXPORT __attribute__((visibility("default")))
30 #endif
31
32 static AVSValue __cdecl CreateFFIndex(AVSValue Args, void* UserData, IScriptEnvironment* Env) {
33 if (!Args[0].Defined())
34 Env->ThrowError("FFIndex: No source specified");
35
36 FFMS_Init(0, 0);
37
38 const char *Source = Args[0].AsString();
39 const char *CacheFile = Args[1].AsString("");
40 int IndexMask = Args[2].AsInt(-1);
41 int ErrorHandling = Args[3].AsInt(FFMS_IEH_IGNORE);
42 bool OverWrite = Args[4].AsBool(false);
43 bool EnableDrefs = Args[5].AsBool(false);
44 bool UseAbsolutePath = Args[6].AsBool(false);
45
46 std::string DefaultCache(Source);
47 DefaultCache.append(".ffindex");
48 if (!strcmp(CacheFile, ""))
49 CacheFile = DefaultCache.c_str();
50
51 ErrorInfo E;
52 FFMS_Index *Index = FFMS_ReadIndex(CacheFile, &E);
53 if (OverWrite || !Index || (Index && FFMS_IndexBelongsToFile(Index, Source, 0) != FFMS_ERROR_SUCCESS)) {
54 FFMS_KeyValuePair LAVFOpts[] = {{ "enable_drefs", EnableDrefs ? "1" : "0" }, { "use_absolute_path", UseAbsolutePath ? "1" : "0" }};
55 FFMS_Indexer *Indexer = FFMS_CreateIndexer2(Source, LAVFOpts, 2, &E);
56 if (!Indexer)
57 Env->ThrowError("FFIndex: %s", E.Buffer);
58
59 // Treat -1 as meaning track numbers above sizeof(int) too
60 if (IndexMask == -1)
61 FFMS_TrackTypeIndexSettings(Indexer, FFMS_TYPE_AUDIO, 1, 0);
62
63 // Apply attributes to remaining tracks (will set the attributes again on some tracks)
64 for (int i = 0; i < sizeof(IndexMask) * 8; i++) {
65 if ((IndexMask >> i) & 1)
66 FFMS_TrackIndexSettings(Indexer, i, 1, 0);
67 }
68
69 Index = FFMS_DoIndexing2(Indexer, ErrorHandling, &E);
70 if (!Index)
71 Env->ThrowError("FFIndex: %s", E.Buffer);
72 if (FFMS_WriteIndex(CacheFile, Index, &E)) {
73 FFMS_DestroyIndex(Index);
74 Env->ThrowError("FFIndex: %s", E.Buffer);
75 }
76 FFMS_DestroyIndex(Index);
77 if (!OverWrite)
78 return AVSValue(1);
79 else
80 return AVSValue(2);
81 } else {
82 FFMS_DestroyIndex(Index);
83 return AVSValue(0);
84 }
85 }
86
87 static AVSValue __cdecl CreateFFVideoSource(AVSValue Args, void* UserData, IScriptEnvironment* Env) {
88 FFMS_Init(0, 0);
89
90 if (!Args[0].Defined())
91 Env->ThrowError("FFVideoSource: No source specified");
92
93 const char *Source = Args[0].AsString();
94 int Track = Args[1].AsInt(-1);
95 bool Cache = Args[2].AsBool(true);
96 const char *CacheFile = Args[3].AsString("");
97 int FPSNum = Args[4].AsInt(-1);
98 int FPSDen = Args[5].AsInt(1);
99 int Threads = Args[6].AsInt(-1);
100 const char *Timecodes = Args[7].AsString("");
101 int SeekMode = Args[8].AsInt(1);
102 int RFFMode = Args[9].AsInt(0);
103 int Width = Args[10].AsInt(0);
104 int Height = Args[11].AsInt(0);
105 const char *Resizer = Args[12].AsString("BICUBIC");
106 const char *ColorSpace = Args[13].AsString("");
107 const char *VarPrefix = Args[14].AsString("");
108
109 if (FPSDen < 1)
110 Env->ThrowError("FFVideoSource: FPS denominator needs to be 1 or higher");
111
112 if (Track <= -2)
113 Env->ThrowError("FFVideoSource: No video track selected");
114
115 if (SeekMode < -1 || SeekMode > 3)
116 Env->ThrowError("FFVideoSource: Invalid seekmode selected");
117
118 if (RFFMode != 0)
119 Env->ThrowError("FFVideoSource: Invalid RFF mode selected");
120
121 if (IsSamePath(Source, Timecodes))
122 Env->ThrowError("FFVideoSource: Timecodes will overwrite the source");
123
124 ErrorInfo E;
125 FFMS_Index *Index = nullptr;
126 std::string DefaultCache;
127 if (Cache) {
128 if (*CacheFile) {
129 if (IsSamePath(Source, CacheFile))
130 Env->ThrowError("FFVideoSource: Cache will overwrite the source");
131 Index = FFMS_ReadIndex(CacheFile, &E);
132 } else {
133 DefaultCache = Source;
134 DefaultCache += ".ffindex";
135 CacheFile = DefaultCache.c_str();
136 Index = FFMS_ReadIndex(CacheFile, &E);
137 // Reindex if the index doesn't match the file and its name wasn't
138 // explicitly given
139 if (Index && FFMS_IndexBelongsToFile(Index, Source, 0) != FFMS_ERROR_SUCCESS) {
140 FFMS_DestroyIndex(Index);
141 Index = 0;
142 }
143 }
144 }
145
146 if (!Index) {
147 FFMS_Indexer *Indexer = FFMS_CreateIndexer(Source, &E);
148 if (!Indexer)
149 Env->ThrowError("FFVideoSource: %s", E.Buffer);
150
151 Index = FFMS_DoIndexing2(Indexer, FFMS_IEH_CLEAR_TRACK, &E);
152 if (!Index)
153 Env->ThrowError("FFVideoSource: %s", E.Buffer);
154
155 if (Cache)
156 if (FFMS_WriteIndex(CacheFile, Index, &E)) {
157 FFMS_DestroyIndex(Index);
158 Env->ThrowError("FFVideoSource: %s", E.Buffer);
159 }
160 }
161
162 if (Track == -1)
163 Track = FFMS_GetFirstIndexedTrackOfType(Index, FFMS_TYPE_VIDEO, &E);
164 if (Track < 0)
165 Env->ThrowError("FFVideoSource: No video track found");
166
167 if (strcmp(Timecodes, "")) {
168 if (FFMS_WriteTimecodes(FFMS_GetTrackFromIndex(Index, Track), Timecodes, &E)) {
169 FFMS_DestroyIndex(Index);
170 Env->ThrowError("FFVideoSource: %s", E.Buffer);
171 }
172 }
173
174 AvisynthVideoSource *Filter;
175
176 try {
177 Filter = new AvisynthVideoSource(Source, Track, Index, FPSNum, FPSDen, Threads, SeekMode, Width, Height, Resizer, ColorSpace, VarPrefix, Env);
178 } catch (...) {
179 FFMS_DestroyIndex(Index);
180 throw;
181 }
182
183 FFMS_DestroyIndex(Index);
184 return Filter;
185 }
186
187 static AVSValue __cdecl CreateFFAudioSource(AVSValue Args, void* UserData, IScriptEnvironment* Env) {
188 FFMS_Init(0, 0);
189
190 if (!Args[0].Defined())
191 Env->ThrowError("FFAudioSource: No source specified");
192
193 const char *Source = Args[0].AsString();
194 int Track = Args[1].AsInt(-1);
195 bool Cache = Args[2].AsBool(true);
196 const char *CacheFile = Args[3].AsString("");
197 int AdjustDelay = Args[4].AsInt(-1);
198 int FillGaps = Args[5].AsInt(-1);
199 double DrcScale = Args[6].AsFloat(0);
200 const char *VarPrefix = Args[7].AsString("");
201
202 if (Track <= -2)
203 Env->ThrowError("FFAudioSource: No audio track selected");
204
205 ErrorInfo E;
206 FFMS_Index *Index = nullptr;
207 std::string DefaultCache;
208 if (Cache) {
209 if (*CacheFile) {
210 if (IsSamePath(Source, CacheFile))
211 Env->ThrowError("FFAudioSource: Cache will overwrite the source");
212 Index = FFMS_ReadIndex(CacheFile, &E);
213 } else {
214 DefaultCache = Source;
215 DefaultCache += ".ffindex";
216 CacheFile = DefaultCache.c_str();
217 Index = FFMS_ReadIndex(CacheFile, &E);
218 // Reindex if the index doesn't match the file and its name wasn't
219 // explicitly given
220 if (Index && FFMS_IndexBelongsToFile(Index, Source, 0) != FFMS_ERROR_SUCCESS) {
221 FFMS_DestroyIndex(Index);
222 Index = 0;
223 }
224 }
225 }
226
227 // Index needs to be remade if it is an unindexed audio track
228 if (Index && Track >= 0 && Track < FFMS_GetNumTracks(Index)
229 && FFMS_GetTrackType(FFMS_GetTrackFromIndex(Index, Track)) == FFMS_TYPE_AUDIO
230 && FFMS_GetNumFrames(FFMS_GetTrackFromIndex(Index, Track)) == 0) {
231 FFMS_DestroyIndex(Index);
232 Index = nullptr;
233 }
234
235 // More complicated for finding a default track, reindex the file if at least one audio track exists
236 if (Index && FFMS_GetFirstTrackOfType(Index, FFMS_TYPE_AUDIO, &E) >= 0
237 && FFMS_GetFirstIndexedTrackOfType(Index, FFMS_TYPE_AUDIO, &E) < 0) {
238 for (int i = 0; i < FFMS_GetNumTracks(Index); i++) {
239 if (FFMS_GetTrackType(FFMS_GetTrackFromIndex(Index, i)) == FFMS_TYPE_AUDIO) {
240 FFMS_DestroyIndex(Index);
241 Index = nullptr;
242 break;
243 }
244 }
245 }
246
247 if (!Index) {
248 FFMS_Indexer *Indexer = FFMS_CreateIndexer(Source, &E);
249 if (!Indexer)
250 Env->ThrowError("FFAudioSource: %s", E.Buffer);
251
252 FFMS_TrackTypeIndexSettings(Indexer, FFMS_TYPE_AUDIO, 1, 0);
253
254 Index = FFMS_DoIndexing2(Indexer, FFMS_IEH_CLEAR_TRACK, &E);
255 if (!Index)
256 Env->ThrowError("FFAudioSource: %s", E.Buffer);
257
258 if (Cache)
259 if (FFMS_WriteIndex(CacheFile, Index, &E)) {
260 FFMS_DestroyIndex(Index);
261 Env->ThrowError("FFAudioSource: %s", E.Buffer);
262 }
263 }
264
265 if (Track == -1)
266 Track = FFMS_GetFirstIndexedTrackOfType(Index, FFMS_TYPE_AUDIO, &E);
267 if (Track < 0)
268 Env->ThrowError("FFAudioSource: No audio track found");
269
270 if (AdjustDelay < -3)
271 Env->ThrowError("FFAudioSource: Invalid delay adjustment specified");
272 if (AdjustDelay >= FFMS_GetNumTracks(Index))
273 Env->ThrowError("FFAudioSource: Invalid track to calculate delay from specified");
274
275 AvisynthAudioSource *Filter;
276
277 try {
278 Filter = new AvisynthAudioSource(Source, Track, Index, AdjustDelay, FillGaps, DrcScale, VarPrefix, Env);
279 } catch (...) {
280 FFMS_DestroyIndex(Index);
281 throw;
282 }
283
284 FFMS_DestroyIndex(Index);
285 return Filter;
286 }
287
288 static AVSValue __cdecl CreateFFmpegSource2(AVSValue Args, void* UserData, IScriptEnvironment* Env) {
289 const char *FFIArgNames[] = { "source", "cachefile", "indexmask", "overwrite", "enable_drefs", "use_absolute_path"};
290 const char *FFVArgNames[] = { "source", "track", "cache", "cachefile", "fpsnum", "fpsden", "threads", "timecodes", "seekmode", "rffmode", "width", "height", "resizer", "colorspace", "varprefix" };
291 const char *FFAArgNames[] = { "source", "track", "cache", "cachefile", "adjustdelay", "fill_gaps", "drc_scale", "varprefix"};
292
293 bool Cache = Args[3].AsBool(true);
294 bool WithAudio = Args[2].AsInt(-2) > -2;
295 if (Cache) {
296 AVSValue FFIArgs[] = { Args[0], Args[4], WithAudio ? -1 : 0, Args[10], Args[17], Args[18]};
297 static_assert((sizeof(FFIArgs) / sizeof(FFIArgs[0])) == (sizeof(FFIArgNames) / sizeof(FFIArgNames[0])), "Arg error");
298 Env->Invoke("FFIndex", AVSValue(FFIArgs, sizeof(FFIArgs) / sizeof(FFIArgs[0])), FFIArgNames);
299 }
300
301 AVSValue FFVArgs[] = { Args[0], Args[1], Args[3], Args[4], Args[5], Args[6], Args[7], Args[8], Args[9], Args[15], Args[11], Args[12], Args[13], Args[14], Args[21] };
302 static_assert((sizeof(FFVArgs) / sizeof(FFVArgs[0])) == (sizeof(FFVArgNames) / sizeof(FFVArgNames[0])), "Arg error");
303 AVSValue Video = Env->Invoke("FFVideoSource", AVSValue(FFVArgs, sizeof(FFVArgs) / sizeof(FFVArgs[0])), FFVArgNames);
304
305 AVSValue Audio;
306 if (WithAudio) {
307 AVSValue FFAArgs[] = { Args[0], Args[2], Args[3], Args[4], Args[16], Args[19], Args[20], Args[21] };
308 static_assert((sizeof(FFAArgs) / sizeof(FFAArgs[0])) == (sizeof(FFAArgNames) / sizeof(FFAArgNames[0])), "Arg error");
309 Audio = Env->Invoke("FFAudioSource", AVSValue(FFAArgs, sizeof(FFAArgs) / sizeof(FFAArgs[0])), FFAArgNames);
310 AVSValue ADArgs[] = { Video, Audio };
311 return Env->Invoke("AudioDubEx", AVSValue(ADArgs, sizeof(ADArgs) / sizeof(ADArgs[0])));
312 } else {
313 return Video;
314 }
315 }
316
317 static AVSValue __cdecl CreateFFImageSource(AVSValue Args, void* UserData, IScriptEnvironment* Env) {
318 const char *FFISArgNames[] = { "source", "width", "height", "resizer", "colorspace", "varprefix", "cache", "seekmode" };
319 AVSValue FFISArgs[] = { Args[0], Args[1], Args[2], Args[3], Args[4], Args[5], false, -1 };
320 static_assert((sizeof(FFISArgs) / sizeof(FFISArgs[0])) == (sizeof(FFISArgNames) / sizeof(FFISArgNames[0])), "Arg error");
321 return Env->Invoke("FFVideoSource", AVSValue(FFISArgs, sizeof(FFISArgs) / sizeof(FFISArgs[0])), FFISArgNames);
322 }
323
324 static AVSValue __cdecl CreateFFCopyrightInfringement(AVSValue Args, void* UserData, IScriptEnvironment* Env) {
325 const char *ArgNames[] = { "source" };
326 Env->Invoke("FFIndex", Args, ArgNames);
327 AVSValue ExArgs[2] = { Env->Invoke("FFVideoSource", Args, ArgNames), Env->Invoke("FFAudioSource", Args, ArgNames) };
328 return Env->Invoke("AudioDubEx", AVSValue(ExArgs, sizeof(ExArgs) / sizeof(ExArgs[0])));
329 }
330
331 static AVSValue __cdecl FFGetLogLevel(AVSValue Args, void* UserData, IScriptEnvironment* Env) {
332 return FFMS_GetLogLevel();
333 }
334
335 static AVSValue __cdecl FFSetLogLevel(AVSValue Args, void* UserData, IScriptEnvironment* Env) {
336 FFMS_SetLogLevel(Args[0].AsInt());
337 return FFMS_GetLogLevel();
338 }
339
340 static AVSValue __cdecl FFGetVersion(AVSValue Args, void* UserData, IScriptEnvironment* Env) {
341 int Version = FFMS_GetVersion();
342 return Env->Sprintf("%d.%d.%d.%d", Version >> 24, (Version & 0xFF0000) >> 16, (Version & 0xFF00) >> 8, Version & 0xFF);
343 }
344
345 const AVS_Linkage *AVS_linkage = nullptr;
346
347 extern "C" AVS_EXPORT const char* __stdcall AvisynthPluginInit3(IScriptEnvironment* Env, const AVS_Linkage* const vectors) {
348 AVS_linkage = vectors;
349
350 Env->AddFunction("FFIndex", "[source]s[cachefile]s[indexmask]i[errorhandling]i[overwrite]b[enable_drefs]b[use_absolute_path]b", CreateFFIndex, nullptr);
351 Env->AddFunction("FFVideoSource", "[source]s[track]i[cache]b[cachefile]s[fpsnum]i[fpsden]i[threads]i[timecodes]s[seekmode]i[rffmode]i[width]i[height]i[resizer]s[colorspace]s[varprefix]s", CreateFFVideoSource, nullptr);
352 Env->AddFunction("FFAudioSource", "[source]s[track]i[cache]b[cachefile]s[adjustdelay]i[fill_gaps]i[drc_scale]f[varprefix]s", CreateFFAudioSource, nullptr);
353
354 Env->AddFunction("FFmpegSource2", "[source]s[vtrack]i[atrack]i[cache]b[cachefile]s[fpsnum]i[fpsden]i[threads]i[timecodes]s[seekmode]i[overwrite]b[width]i[height]i[resizer]s[colorspace]s[rffmode]i[adjustdelay]i[enable_drefs]b[use_absolute_path]b[fill_gaps]i[drc_scale]f[varprefix]s", CreateFFmpegSource2, nullptr);
355 Env->AddFunction("FFMS2", "[source]s[vtrack]i[atrack]i[cache]b[cachefile]s[fpsnum]i[fpsden]i[threads]i[timecodes]s[seekmode]i[overwrite]b[width]i[height]i[resizer]s[colorspace]s[rffmode]i[adjustdelay]i[enable_drefs]b[use_absolute_path]b[fill_gaps]i[drc_scale]f[varprefix]s", CreateFFmpegSource2, nullptr);
356
357 Env->AddFunction("FFImageSource", "[source]s[width]i[height]i[resizer]s[colorspace]s[varprefix]s", CreateFFImageSource, nullptr);
358 Env->AddFunction("FFCopyrightInfringement", "[source]s", CreateFFCopyrightInfringement, nullptr);
359
360 Env->AddFunction("FFGetLogLevel", "", FFGetLogLevel, nullptr);
361 Env->AddFunction("FFSetLogLevel", "i", FFSetLogLevel, nullptr);
362 Env->AddFunction("FFGetVersion", "", FFGetVersion, nullptr);
363
364 return "FFmpegSource - The Second Coming V2.0 Final";
365 }