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