]> git.sesse.net Git - nageru/blob - x264_dynamic.cpp
Rename ui_foo.ui to foo.ui; seemingly, it is more standard.
[nageru] / x264_dynamic.cpp
1 #include "x264_dynamic.h"
2
3 #include <assert.h>
4 #include <dlfcn.h>
5 #include <link.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include <string>
11
12 using namespace std;
13
14 X264Dynamic load_x264_for_bit_depth(unsigned depth)
15 {
16         X264Dynamic dyn;
17 #if defined(X264_BIT_DEPTH) && X264_BIT_DEPTH == 0
18         bool suitable = true;  // x264 compiled to support all bit depths.
19 #elif defined(X264_BIT_DEPTH)
20         bool suitable = X264_BIT_DEPTH >= depth;
21 #else
22         bool suitable = unsigned(x264_bit_depth) >= depth;
23 #endif
24         if (suitable) {
25                 // Just use the one we are linked to.
26                 dyn.handle = nullptr;
27                 dyn.x264_encoder_close = x264_encoder_close;
28                 dyn.x264_encoder_delayed_frames = x264_encoder_delayed_frames;
29                 dyn.x264_encoder_encode = x264_encoder_encode;
30                 dyn.x264_encoder_headers = x264_encoder_headers;
31                 dyn.x264_encoder_open = x264_encoder_open;
32                 dyn.x264_encoder_parameters = x264_encoder_parameters;
33                 dyn.x264_encoder_reconfig = x264_encoder_reconfig;
34                 dyn.x264_param_apply_profile = x264_param_apply_profile;
35                 dyn.x264_param_default_preset = x264_param_default_preset;
36                 dyn.x264_param_parse = x264_param_parse;
37                 dyn.x264_picture_init = x264_picture_init;
38                 return dyn;
39         }
40
41         // Uh-oh, our currently loaded library doesn't have the required support.
42         // Let's try to dynamically load a 10-bit version; in particular, Debian
43         // has a version in /usr/lib/x86_64-linux-gnu/x264-10bit/libx264.so.<soname>,
44         // so we try to figure out where our libx264 comes from, and modify that path.
45         string x264_dir, x264_suffix;
46         void *handle = dlopen(nullptr, RTLD_NOW);
47         link_map *m;
48         int err = dlinfo(handle, RTLD_DI_LINKMAP, &m);
49         assert(err != -1);
50         for ( ; m != nullptr; m = m->l_next) {
51                 if (m->l_name == nullptr) {
52                         continue;
53                 }
54                 const char *ptr = strstr(m->l_name, "/libx264.so.");
55                 if (ptr != nullptr) {
56                         x264_dir = string(m->l_name, ptr - m->l_name);
57                         x264_suffix = string(ptr, (m->l_name + strlen(m->l_name)) - ptr);
58                         break;
59                 }
60         }
61         dlclose(handle);
62
63         if (x264_dir.empty()) {
64                 fprintf(stderr, "ERROR: Requested %d-bit x264, but is not linked to such an x264, and could not find one.\n",
65                         depth);
66                 exit(1);
67         }
68
69         string x264_10b_string = x264_dir + "/x264-10bit" + x264_suffix;
70         void *x264_dlhandle = dlopen(x264_10b_string.c_str(), RTLD_NOW);
71         if (x264_dlhandle == nullptr) {
72                 fprintf(stderr, "ERROR: Requested %d-bit x264, but is not linked to such an x264, and %s would not load.\n",
73                         depth, x264_10b_string.c_str());
74                 exit(1);
75         }
76
77         dyn.handle = x264_dlhandle;
78         dyn.x264_encoder_close = (decltype(::x264_encoder_close) *)dlsym(x264_dlhandle, "x264_encoder_close");
79         dyn.x264_encoder_delayed_frames = (decltype(::x264_encoder_delayed_frames) *)dlsym(x264_dlhandle, "x264_encoder_delayed_frames");
80         dyn.x264_encoder_encode = (decltype(::x264_encoder_encode) *)dlsym(x264_dlhandle, "x264_encoder_encode");
81         dyn.x264_encoder_headers = (decltype(::x264_encoder_headers) *)dlsym(x264_dlhandle, "x264_encoder_headers");
82         char x264_encoder_open_symname[256];
83         snprintf(x264_encoder_open_symname, sizeof(x264_encoder_open_symname), "x264_encoder_open_%d", X264_BUILD);
84         dyn.x264_encoder_open = (decltype(::x264_encoder_open) *)dlsym(x264_dlhandle, x264_encoder_open_symname);
85         dyn.x264_encoder_parameters = (decltype(::x264_encoder_parameters) *)dlsym(x264_dlhandle, "x264_encoder_parameters");
86         dyn.x264_encoder_reconfig = (decltype(::x264_encoder_reconfig) *)dlsym(x264_dlhandle, "x264_encoder_reconfig");
87         dyn.x264_param_apply_profile = (decltype(::x264_param_apply_profile) *)dlsym(x264_dlhandle, "x264_param_apply_profile");
88         dyn.x264_param_default_preset = (decltype(::x264_param_default_preset) *)dlsym(x264_dlhandle, "x264_param_default_preset");
89         dyn.x264_param_parse = (decltype(::x264_param_parse) *)dlsym(x264_dlhandle, "x264_param_parse");
90         dyn.x264_picture_init = (decltype(::x264_picture_init) *)dlsym(x264_dlhandle, "x264_picture_init");
91         return dyn;
92 }