History

avformat/vapoursynth: load library at runtime

Stefan Oltmanns committed 2 years ago

Commit d42cd5b1 parent eac611f2 files · +64 −3Browse files

Unified · Split

2 files
  1. configure +1 −1
  2. libavformat/vapoursynth.c +63 −2
configure+1−1
@@ -7090,7 +7090,7 @@ enabled rkmpp && { require_pkg_config rkmpp rockchip_mpp rockchip/r · 7089 unmodified lines
70907090 { enabled libdrm ||
70917091 die "ERROR: rkmpp requires --enable-libdrm"; }
70927092 }
7093−enabled vapoursynth && require_pkg_config vapoursynth "vapoursynth-script >= 55" VSScript4.h getVSScriptAPI
7093+enabled vapoursynth && require_headers "vapoursynth/VSScript4.h vapoursynth/VapourSynth4.h"
70947094
70957095
70967096 if enabled gcrypt; then
libavformat/vapoursynth.c+63−2
@@ -25,7 +25,7 @@ · 24 unmodified lines
2525
2626 #include <limits.h>
2727
28−#include <VSScript4.h>
28+#include <vapoursynth/VSScript4.h>
2929
3030 #include "libavutil/avassert.h"
3131 #include "libavutil/avstring.h"
@@ -39,11 +39,26 @@ · 7 unmodified lines
3939 #include "demux.h"
4040 #include "internal.h"
4141
42+/* Platform-specific directives. */
43+#ifdef _WIN32
44+ #include <windows.h>
45+ #include "compat/w32dlfcn.h"
46+ #include "libavutil/wchar_filename.h"
47+ #undef EXTERN_C
48+ #define VSSCRIPT_LIB "VSScript.dll"
49+#else
50+ #include <dlfcn.h>
51+ #define VSSCRIPT_NAME "libvapoursynth-script"
52+ #define VSSCRIPT_LIB VSSCRIPT_NAME SLIBSUF
53+#endif
54+
4255 struct VSState {
4356 const VSSCRIPTAPI *vssapi;
4457 VSScript *vss;
4558 };
4659
60+typedef const VSSCRIPTAPI *(*VSScriptGetAPIFunc)(int version);
61+
4762 typedef struct VSContext {
4863 const AVClass *class;
4964
@@ -51,6 +66,7 @@ typedef struct VSContext { · 1 unmodified line
5166
5267 const VSSCRIPTAPI *vssapi;
5368 const VSAPI *vsapi;
69+ void *vslibrary;
5470
5571 VSNode *outnode;
5672 int is_cfr;
@@ -70,6 +86,40 @@ static const AVOption options[] = { · 13 unmodified lines
7086 {NULL}
7187 };
7288
89+static av_cold void* vs_load_library(VSScriptGetAPIFunc *get_vssapi)
90+{
91+ void *vslibrary = NULL;
92+#ifdef _WIN32
93+ const HKEY hkeys[] = {HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE};
94+ LONG r;
95+ WCHAR vss_path[512];
96+ DWORD buf_size = sizeof(vss_path) - 2;
97+ char *vss_path_utf8;
98+ int i;
99+
100+ for (i = 0; i < FF_ARRAY_ELEMS(hkeys); i++) {
101+ if ((r = RegGetValueW(hkeys[i], L"SOFTWARE\\VapourSynth",
102+ L"VSScriptDLL", RRF_RT_REG_SZ, NULL,
103+ &vss_path, &buf_size)) == ERROR_SUCCESS)
104+ break;
105+ }
106+ if (r == ERROR_SUCCESS && wchartoutf8(vss_path, &vss_path_utf8) == 0) {
107+ vslibrary = dlopen(vss_path_utf8, RTLD_NOW | RTLD_GLOBAL);
108+ av_free(vss_path_utf8);
109+ }
110+ else
111+#endif
112+ vslibrary = dlopen(VSSCRIPT_LIB, RTLD_NOW | RTLD_GLOBAL);
113+
114+ if (vslibrary != NULL) {
115+ if (!(*get_vssapi = (VSScriptGetAPIFunc)dlsym(vslibrary, "getVSScriptAPI"))) {
116+ dlclose(vslibrary);
117+ return NULL;
118+ }
119+ }
120+ return vslibrary;
121+}
122+
73123 static void free_vss_state(void *opaque, uint8_t *data)
74124 {
75125 struct VSState *vss = opaque;
@@ -91,6 +141,9 @@ static av_cold int read_close_vs(AVFormatContext *s) · 15 unmodified lines
91141 vs->vsapi = NULL;
92142 vs->outnode = NULL;
93143
144+ if (vs->vslibrary)
145+ dlclose(vs->vslibrary);
146+
94147 return 0;
95148 }
96149
@@ -170,6 +223,7 @@ static av_cold int read_header_vs(AVFormatContext *s) · 73 unmodified lines
170223 AVStream *st;
171224 AVIOContext *pb = s->pb;
172225 VSContext *vs = s->priv_data;
226+ VSScriptGetAPIFunc get_vssapi;
173227 int64_t sz = avio_size(pb);
174228 char *buf = NULL;
175229 char dummy;
@@ -178,7 +232,14 @@ static av_cold int read_header_vs(AVFormatContext *s) · 2 unmodified lines
178232 struct VSState *vss_state;
179233 int err = 0;
180234
181− if (!(vs->vssapi = getVSScriptAPI(VSSCRIPT_API_VERSION))) {
235+ if (!(vs->vslibrary = vs_load_library(&get_vssapi))) {
236+ av_log(s, AV_LOG_ERROR, "Could not open " VSSCRIPT_LIB ". "
237+ "Check VapourSynth installation.\n");
238+ err = AVERROR_EXTERNAL;
239+ goto done;
240+ }
241+
242+ if (!(vs->vssapi = get_vssapi(VSSCRIPT_API_VERSION))) {
182243 av_log(s, AV_LOG_ERROR, "Failed to initialize VSScript (possibly PYTHONPATH not set).\n");
183244 err = AVERROR_EXTERNAL;
184245 goto done;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t