Debian Codegraph @

src/vapoursynth/vapoursynth4.cpp

raw symbols SCIP
1 // Copyright (c) 2012-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 "ffms.h"
22 #include "vapoursource4.h"
23 #include "VSHelper4.h"
24 #include "../core/utils.h"
25
26 #include <cstdio>
27 #include <cstring>
28 #include <string>
29 #include <set>
30
31 static void VS_CC CreateIndex(const VSMap *in, VSMap *out, void *, VSCore *, const VSAPI *vsapi) {
32 FFMS_Init(0, 0);
33
34 char ErrorMsg[1024];
35 FFMS_ErrorInfo E;
36 E.Buffer = ErrorMsg;
37 E.BufferSize = sizeof(ErrorMsg);
38 int err;
39
40 std::set<int> IndexTracks;
41
42 const char *Source = vsapi->mapGetData(in, "source", 0, nullptr);
43 const char *CacheFile = vsapi->mapGetData(in, "cachefile", 0, &err);
44
45 int NumIndexTracks = vsapi->mapNumElements(in, "indextracks");
46 bool IndexAllTracks = (NumIndexTracks == 1) && (vsapi->mapGetIntSaturated(in, "indextracks", 0, nullptr) == -1);
47 if (!IndexAllTracks) {
48 for (int i = 0; i < NumIndexTracks; i++) {
49 int Track = vsapi->mapGetIntSaturated(in, "indextracks", i, nullptr);
50 IndexTracks.insert(Track);
51 }
52 }
53
54 int ErrorHandling = vsapi->mapGetIntSaturated(in, "errorhandling", 0, &err);
55 if (err)
56 ErrorHandling = FFMS_IEH_IGNORE;
57 bool OverWrite = !!vsapi->mapGetInt(in, "overwrite", 0, &err);
58
59 std::string DefaultCache(Source);
60 if (!CacheFile || !strcmp(CacheFile, "")) {
61 DefaultCache.append(".ffindex");
62 CacheFile = DefaultCache.c_str();
63 }
64
65 bool EnableDrefs = !!vsapi->mapGetInt(in, "enable_drefs", 0, &err);
66 bool UseAbsolutePath = !!vsapi->mapGetInt(in, "use_absolute_path", 0, &err);
67
68 FFMS_Index *Index = FFMS_ReadIndex(CacheFile, &E);
69 if (OverWrite || !Index || (Index && FFMS_IndexBelongsToFile(Index, Source, nullptr) != FFMS_ERROR_SUCCESS)) {
70 FFMS_KeyValuePair LAVFOpts[] = {{ "enable_drefs", EnableDrefs ? "1" : "0" }, { "use_absolute_path", UseAbsolutePath ? "1" : "0" }};
71 FFMS_Indexer *Indexer = FFMS_CreateIndexer2(Source, LAVFOpts, 2, &E);
72 if (!Indexer) {
73 FFMS_DestroyIndex(Index);
74 return vsapi->mapSetError(out, (std::string("Index: ") + E.Buffer).c_str());
75 }
76
77 if (IndexAllTracks) {
78 FFMS_TrackTypeIndexSettings(Indexer, FFMS_TYPE_AUDIO, 1, 0);
79 } else {
80 for (int i : IndexTracks)
81 FFMS_TrackIndexSettings(Indexer, i, 1, 0);
82 }
83
84 Index = FFMS_DoIndexing2(Indexer, ErrorHandling, &E);
85 if (!Index)
86 return vsapi->mapSetError(out, (std::string("Index: ") + E.Buffer).c_str());
87 if (FFMS_WriteIndex(CacheFile, Index, &E)) {
88 FFMS_DestroyIndex(Index);
89 return vsapi->mapSetError(out, (std::string("Index: ") + E.Buffer).c_str());
90 }
91 FFMS_DestroyIndex(Index);
92 if (!OverWrite)
93 vsapi->mapSetData(out, "result", "Index generated", -1, dtUtf8, maReplace);
94 else
95 vsapi->mapSetData(out, "result", "Index generated (forced overwrite)", -1, dtUtf8, maReplace);
96 } else {
97 FFMS_DestroyIndex(Index);
98 vsapi->mapSetData(out, "result", "Valid index already exists", -1, dtUtf8, maReplace);
99 }
100 }
101
102 static void VS_CC CreateSource(const VSMap *in, VSMap *out, void *, VSCore *core, const VSAPI *vsapi) {
103 FFMS_Init(0, 0);
104
105 char ErrorMsg[1024];
106 FFMS_ErrorInfo E;
107 E.Buffer = ErrorMsg;
108 E.BufferSize = sizeof(ErrorMsg);
109 int err;
110
111 const char *Source = vsapi->mapGetData(in, "source", 0, nullptr);
112 int Track = vsapi->mapGetIntSaturated(in, "track", 0, &err);
113 if (err)
114 Track = -1;
115 bool Cache = !!vsapi->mapGetInt(in, "cache", 0, &err);
116 if (err)
117 Cache = true;
118 const char *CacheFile = vsapi->mapGetData(in, "cachefile", 0, &err);
119 int FPSNum = vsapi->mapGetIntSaturated(in, "fpsnum", 0, &err);
120 if (err)
121 FPSNum = -1;
122 int FPSDen = vsapi->mapGetIntSaturated(in, "fpsden", 0, &err);
123 if (err)
124 FPSDen = 1;
125 int Threads = vsapi->mapGetIntSaturated(in, "threads", 0, &err);
126 const char *Timecodes = vsapi->mapGetData(in, "timecodes", 0, &err);
127 int SeekMode = vsapi->mapGetIntSaturated(in, "seekmode", 0, &err);
128 if (err)
129 SeekMode = FFMS_SEEK_NORMAL;
130 int Width = vsapi->mapGetIntSaturated(in, "width", 0, &err);
131 int Height = vsapi->mapGetIntSaturated(in, "height", 0, &err);
132 const char *Resizer = vsapi->mapGetData(in, "resizer", 0, &err);
133 if (err)
134 Resizer = "BICUBIC";
135 int Format = vsapi->mapGetIntSaturated(in, "format", 0, &err);
136
137 bool OutputAlpha = !!vsapi->mapGetInt(in, "alpha", 0, &err);
138
139 if (FPSDen < 1)
140 return vsapi->mapSetError(out, "Source: FPS denominator needs to be 1 or higher");
141 if (Track <= -2)
142 return vsapi->mapSetError(out, "Source: No video track selected");
143 if (SeekMode < -1 || SeekMode > 3)
144 return vsapi->mapSetError(out, "Source: Invalid seekmode selected");
145 if (Timecodes && IsSamePath(Source, Timecodes))
146 return vsapi->mapSetError(out, "Source: Timecodes will overwrite the source");
147
148 FFMS_Index *Index = nullptr;
149 std::string DefaultCache;
150 if (Cache) {
151 if (CacheFile && *CacheFile) {
152 if (IsSamePath(Source, CacheFile))
153 return vsapi->mapSetError(out, "Source: Cache will overwrite the source");
154 Index = FFMS_ReadIndex(CacheFile, &E);
155 } else {
156 DefaultCache = Source;
157 DefaultCache += ".ffindex";
158 CacheFile = DefaultCache.c_str();
159 Index = FFMS_ReadIndex(CacheFile, &E);
160 // Reindex if the index doesn't match the file and its name wasn't
161 // explicitly given
162 if (Index && FFMS_IndexBelongsToFile(Index, Source, nullptr) != FFMS_ERROR_SUCCESS) {
163 FFMS_DestroyIndex(Index);
164 Index = nullptr;
165 }
166 }
167 }
168
169 if (!Index) {
170 FFMS_Indexer *Indexer = FFMS_CreateIndexer(Source, &E);
171 if (!Indexer)
172 return vsapi->mapSetError(out, (std::string("Index: ") + E.Buffer).c_str());
173
174 Index = FFMS_DoIndexing2(Indexer, FFMS_IEH_CLEAR_TRACK, &E);
175 if (!Index)
176 return vsapi->mapSetError(out, (std::string("Index: ") + E.Buffer).c_str());
177
178 if (Cache)
179 if (FFMS_WriteIndex(CacheFile, Index, &E)) {
180 FFMS_DestroyIndex(Index);
181 return vsapi->mapSetError(out, (std::string("Index: ") + E.Buffer).c_str());
182 }
183 }
184
185 if (Track == -1)
186 Track = FFMS_GetFirstIndexedTrackOfType(Index, FFMS_TYPE_VIDEO, &E);
187 if (Track < 0) {
188 FFMS_DestroyIndex(Index);
189 return vsapi->mapSetError(out, "Source: No video track found");
190 }
191
192 if (Timecodes && strcmp(Timecodes, "")) {
193 if (FFMS_WriteTimecodes(FFMS_GetTrackFromIndex(Index, Track), Timecodes, &E)) {
194 FFMS_DestroyIndex(Index);
195 return vsapi->mapSetError(out, (std::string("Index: ") + E.Buffer).c_str());
196 }
197 }
198
199 VSVideoSource4 *vs;
200 try {
201 vs = new VSVideoSource4(Source, Track, Index, FPSNum, FPSDen, Threads, SeekMode, Width, Height, Resizer, Format, OutputAlpha, vsapi, core);
202 } catch (std::exception const& e) {
203 FFMS_DestroyIndex(Index);
204 return vsapi->mapSetError(out, e.what());
205 }
206
207 VSNode *node = vsapi->createVideoFilter2("Source", vs->GetVideoInfo(), VSVideoSource4::GetFrame, VSVideoSource4::Free, fmUnordered, nullptr, 0, vs, core);
208 vs->SetCacheThreshold(vsapi->setLinearFilter(node));
209 vsapi->mapConsumeNode(out, "clip", node, maAppend);
210
211 FFMS_DestroyIndex(Index);
212 }
213
214 static void VS_CC GetLogLevel(const VSMap *, VSMap *out, void *, VSCore *, const VSAPI *vsapi) {
215 vsapi->mapSetInt(out, "level", FFMS_GetLogLevel(), maReplace);
216 }
217
218 static void VS_CC SetLogLevel(const VSMap *in, VSMap *out, void *, VSCore *, const VSAPI *vsapi) {
219 FFMS_SetLogLevel(vsapi->mapGetIntSaturated(in, "level", 0, nullptr));
220 vsapi->mapSetInt(out, "level", FFMS_GetLogLevel(), maReplace);
221 }
222
223 static void VS_CC GetVersion(const VSMap *, VSMap *out, void *, VSCore *, const VSAPI *vsapi) {
224 int Version = FFMS_GetVersion();
225 char buf[100];
226 sprintf(buf, "%d.%d.%d.%d", Version >> 24, (Version & 0xFF0000) >> 16, (Version & 0xFF00) >> 8, Version & 0xFF);
227 vsapi->mapSetData(out, "version", buf, -1, maReplace, dtUtf8);
228 }
229
230 VS_EXTERNAL_API(void) VapourSynthPluginInit2(VSPlugin *plugin, const VSPLUGINAPI *vspapi) {
231 vspapi->configPlugin("com.vapoursynth.ffms2", "ffms2", "FFmpegSource 2 for VapourSynth", FFMS_GetVersion(), VAPOURSYNTH_API_VERSION, 0, plugin);
232 vspapi->registerFunction("Index", "source:data;cachefile:data:opt;indextracks:int[]:opt;errorhandling:int:opt;overwrite:int:opt;enable_drefs:int:opt;use_absolute_path:int:opt;", "result:data;", CreateIndex, nullptr, plugin);
233 vspapi->registerFunction("Source", "source:data;track:int:opt;cache:int:opt;cachefile:data:opt;fpsnum:int:opt;fpsden:int:opt;threads:int:opt;timecodes:data:opt;seekmode:int:opt;width:int:opt;height:int:opt;resizer:data:opt;format:int:opt;alpha:int:opt;", "clip:vnode;", CreateSource, nullptr, plugin);
234 vspapi->registerFunction("GetLogLevel", "", "level:int;", GetLogLevel, nullptr, plugin);
235 vspapi->registerFunction("SetLogLevel", "level:int;", "level:int;", SetLogLevel, nullptr, plugin);
236 vspapi->registerFunction("Version", "", "version:data;", GetVersion, nullptr, plugin);
237 }