[x265] [PATCH 1 of 4] api: allow libx265 to forward x265_api_get() calls

Steve Borho steve at borho.org
Tue Apr 28 22:39:58 CEST 2015


# HG changeset patch
# User Steve Borho <steve at borho.org>
# Date 1430251554 18000
#      Tue Apr 28 15:05:54 2015 -0500
# Node ID 0183918ae416a5201a0f5048f87936bde044c9cd
# Parent  c4d9ee2cef03ef74f5623784d514ffcdf725bec4
api: allow libx265 to forward x265_api_get() calls

By adding dynamic binding to x265_api_get() within libx265 itself, we remove the
need for a shim library. Now any libx265 library can forward requests for APIs
supporting a different bitdepth.  The library name is hard-coded as libx265_main
or libx265_main10 depending on the requested bit depth (making it simple to add
libx265_main12 in the future).

diff -r c4d9ee2cef03 -r 0183918ae416 source/CMakeLists.txt
--- a/source/CMakeLists.txt	Tue Apr 28 14:34:45 2015 -0500
+++ b/source/CMakeLists.txt	Tue Apr 28 15:05:54 2015 -0500
@@ -65,6 +65,10 @@
     if(LIBRT)
         list(APPEND PLATFORM_LIBS rt)
     endif()
+    find_library(LIBDL dl)
+    if(LIBDL)
+        list(APPEND PLATFORM_LIBS dl)
+    endif()
     find_package(Numa)
     if(NUMA_FOUND)
         link_directories(${NUMA_LIBRARY_DIR})
diff -r c4d9ee2cef03 -r 0183918ae416 source/encoder/api.cpp
--- a/source/encoder/api.cpp	Tue Apr 28 14:34:45 2015 -0500
+++ b/source/encoder/api.cpp	Tue Apr 28 15:05:54 2015 -0500
@@ -253,11 +253,64 @@
     x265_max_bit_depth,
 };
 
+typedef const x265_api* (*api_get_func)(int bitDepth);
+
+#define xstr(s) str(s)
+#define str(s) #s
+
+#if _WIN32
+#define ext ".dll"
+#elif MACOS
+#include <dlfcn.h>
+#define ext ".dylib"
+#else
+#include <dlfcn.h>
+#define ext ".so"
+#endif
+
 extern "C"
 const x265_api* x265_api_get(int bitDepth)
 {
     if (bitDepth && bitDepth != X265_DEPTH)
+    {
+        const char* libname = NULL;
+        const char* method = "x265_api_get_" xstr(X265_BUILD);
+
+        if (bitDepth == 10)
+            libname = "libx265_main10" ext;
+        else if (bitDepth == 8)
+            libname = "libx265_main" ext;
+        else
+            return NULL;
+
+#if _WIN32
+        HMODULE h = LoadLibraryA(libname);
+        if (h)
+        {
+            api_get_func get = (api_get_func)GetProcAddress(h, method);
+            if (get)
+                return get(bitDepth);
+            else
+                x265_log(NULL, X265_LOG_WARNING, "Unable to bind %s from %s\n", method, libname);
+        }
+        else
+            x265_log(NULL, X265_LOG_WARNING, "Unable to open %s\n", libname);
+#else
+        void* h = dlopen(libname, RTLD_LAZY | RTLD_LOCAL);
+        if (h)
+        {
+            api_get_func get = (api_get_func)dlsym(h, method);
+            if (get)
+                return get(bitDepth);
+            else
+                x265_log(NULL, X265_LOG_WARNING, "Unable to bind %s from %s\n", method, libname);
+        }
+        else
+            x265_log(NULL, X265_LOG_WARNING, "Unable to open %s\n", libname);
+#endif
+
         return NULL;
+    }
 
     return &libapi;
 }


More information about the x265-devel mailing list