File size: 14,115 Bytes
034d0a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright 2023 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <memory>

#include "common/dynamic_library/dynamic_library.h"
#include "common/dynamic_library/ffmpeg.h"
#include "common/logging/log.h"

namespace DynamicLibrary::FFmpeg {

// avutil
av_buffer_ref_func av_buffer_ref;
av_buffer_unref_func av_buffer_unref;
av_d2q_func av_d2q;
av_dict_count_func av_dict_count;
av_dict_get_func av_dict_get;
av_dict_get_string_func av_dict_get_string;
av_dict_set_func av_dict_set;
av_frame_alloc_func av_frame_alloc;
av_frame_free_func av_frame_free;
av_frame_unref_func av_frame_unref;
av_freep_func av_freep;
av_get_bytes_per_sample_func av_get_bytes_per_sample;
av_get_pix_fmt_func av_get_pix_fmt;
av_get_pix_fmt_name_func av_get_pix_fmt_name;
av_get_sample_fmt_name_func av_get_sample_fmt_name;
av_hwdevice_ctx_create_func av_hwdevice_ctx_create;
av_hwdevice_get_hwframe_constraints_func av_hwdevice_get_hwframe_constraints;
av_hwframe_constraints_free_func av_hwframe_constraints_free;
av_hwframe_ctx_alloc_func av_hwframe_ctx_alloc;
av_hwframe_ctx_init_func av_hwframe_ctx_init;
av_hwframe_get_buffer_func av_hwframe_get_buffer;
av_hwframe_transfer_data_func av_hwframe_transfer_data;
av_int_list_length_for_size_func av_int_list_length_for_size;
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(56, 53, 100) // lavu 56.53.100
av_opt_child_class_iterate_func av_opt_child_class_iterate;
#else
av_opt_child_class_next_func av_opt_child_class_next;
#endif
av_opt_next_func av_opt_next;
av_opt_set_bin_func av_opt_set_bin;
av_pix_fmt_desc_get_func av_pix_fmt_desc_get;
av_pix_fmt_desc_next_func av_pix_fmt_desc_next;
av_sample_fmt_is_planar_func av_sample_fmt_is_planar;
av_samples_alloc_array_and_samples_func av_samples_alloc_array_and_samples;
av_strdup_func av_strdup;
avutil_version_func avutil_version;

// avcodec
av_codec_is_encoder_func av_codec_is_encoder;
av_codec_iterate_func av_codec_iterate;
av_init_packet_func av_init_packet;
av_packet_alloc_func av_packet_alloc;
av_packet_free_func av_packet_free;
av_packet_rescale_ts_func av_packet_rescale_ts;
av_parser_close_func av_parser_close;
av_parser_init_func av_parser_init;
av_parser_parse2_func av_parser_parse2;
avcodec_alloc_context3_func avcodec_alloc_context3;
avcodec_descriptor_next_func avcodec_descriptor_next;
avcodec_find_decoder_func avcodec_find_decoder;
avcodec_find_encoder_by_name_func avcodec_find_encoder_by_name;
avcodec_free_context_func avcodec_free_context;
avcodec_get_class_func avcodec_get_class;
avcodec_get_hw_config_func avcodec_get_hw_config;
avcodec_open2_func avcodec_open2;
avcodec_parameters_from_context_func avcodec_parameters_from_context;
avcodec_receive_frame_func avcodec_receive_frame;
avcodec_receive_packet_func avcodec_receive_packet;
avcodec_send_frame_func avcodec_send_frame;
avcodec_send_packet_func avcodec_send_packet;
avcodec_version_func avcodec_version;

// avfilter
av_buffersink_get_frame_func av_buffersink_get_frame;
av_buffersrc_add_frame_func av_buffersrc_add_frame;
avfilter_get_by_name_func avfilter_get_by_name;
avfilter_graph_alloc_func avfilter_graph_alloc;
avfilter_graph_config_func avfilter_graph_config;
avfilter_graph_create_filter_func avfilter_graph_create_filter;
avfilter_graph_free_func avfilter_graph_free;
avfilter_graph_parse_ptr_func avfilter_graph_parse_ptr;
avfilter_inout_alloc_func avfilter_inout_alloc;
avfilter_inout_free_func avfilter_inout_free;
avfilter_version_func avfilter_version;

// avformat
av_guess_format_func av_guess_format;
av_interleaved_write_frame_func av_interleaved_write_frame;
av_muxer_iterate_func av_muxer_iterate;
av_write_trailer_func av_write_trailer;
avformat_alloc_output_context2_func avformat_alloc_output_context2;
avformat_free_context_func avformat_free_context;
avformat_get_class_func avformat_get_class;
avformat_network_init_func avformat_network_init;
avformat_new_stream_func avformat_new_stream;
avformat_query_codec_func avformat_query_codec;
avformat_write_header_func avformat_write_header;
avformat_version_func avformat_version;
avio_closep_func avio_closep;
avio_open_func avio_open;

// swresample
#if LIBSWRESAMPLE_VERSION_INT >= AV_VERSION_INT(4, 5, 100)
swr_alloc_set_opts2_func swr_alloc_set_opts2;
#else
swr_alloc_set_opts_func swr_alloc_set_opts;
#endif
swr_convert_func swr_convert;
swr_free_func swr_free;
swr_init_func swr_init;
swresample_version_func swresample_version;

static std::unique_ptr<Common::DynamicLibrary> avutil;
static std::unique_ptr<Common::DynamicLibrary> avcodec;
static std::unique_ptr<Common::DynamicLibrary> avfilter;
static std::unique_ptr<Common::DynamicLibrary> avformat;
static std::unique_ptr<Common::DynamicLibrary> swresample;

#define LOAD_SYMBOL(library, name)                                                                 \
    any_failed = any_failed || (name = library->GetSymbol<name##_func>(#name)) == nullptr

static bool LoadAVUtil() {
    if (avutil) {
        return true;
    }

    avutil = std::make_unique<Common::DynamicLibrary>("avutil", LIBAVUTIL_VERSION_MAJOR);
    if (!avutil->IsLoaded()) {
        LOG_WARNING(Common, "Could not dynamically load libavutil: {}", avutil->GetLoadError());
        avutil.reset();
        return false;
    }

    auto any_failed = false;

    LOAD_SYMBOL(avutil, avutil_version);

    auto major_version = AV_VERSION_MAJOR(avutil_version());
    if (major_version != LIBAVUTIL_VERSION_MAJOR) {
        LOG_WARNING(Common, "libavutil version {} does not match supported version {}.",
                    major_version, LIBAVUTIL_VERSION_MAJOR);
        avutil.reset();
        return false;
    }

    LOAD_SYMBOL(avutil, av_buffer_ref);
    LOAD_SYMBOL(avutil, av_buffer_unref);
    LOAD_SYMBOL(avutil, av_d2q);
    LOAD_SYMBOL(avutil, av_dict_count);
    LOAD_SYMBOL(avutil, av_dict_get);
    LOAD_SYMBOL(avutil, av_dict_get_string);
    LOAD_SYMBOL(avutil, av_dict_set);
    LOAD_SYMBOL(avutil, av_frame_alloc);
    LOAD_SYMBOL(avutil, av_frame_free);
    LOAD_SYMBOL(avutil, av_frame_unref);
    LOAD_SYMBOL(avutil, av_freep);
    LOAD_SYMBOL(avutil, av_get_bytes_per_sample);
    LOAD_SYMBOL(avutil, av_get_pix_fmt);
    LOAD_SYMBOL(avutil, av_get_pix_fmt_name);
    LOAD_SYMBOL(avutil, av_get_sample_fmt_name);
    LOAD_SYMBOL(avutil, av_hwdevice_ctx_create);
    LOAD_SYMBOL(avutil, av_hwdevice_get_hwframe_constraints);
    LOAD_SYMBOL(avutil, av_hwframe_constraints_free);
    LOAD_SYMBOL(avutil, av_hwframe_ctx_alloc);
    LOAD_SYMBOL(avutil, av_hwframe_ctx_init);
    LOAD_SYMBOL(avutil, av_hwframe_get_buffer);
    LOAD_SYMBOL(avutil, av_hwframe_transfer_data);
    LOAD_SYMBOL(avutil, av_int_list_length_for_size);
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(56, 53, 100) // lavu 56.53.100
    LOAD_SYMBOL(avutil, av_opt_child_class_iterate);
#else
    LOAD_SYMBOL(avutil, av_opt_child_class_next);
#endif
    LOAD_SYMBOL(avutil, av_opt_next);
    LOAD_SYMBOL(avutil, av_opt_set_bin);
    LOAD_SYMBOL(avutil, av_pix_fmt_desc_get);
    LOAD_SYMBOL(avutil, av_pix_fmt_desc_next);
    LOAD_SYMBOL(avutil, av_sample_fmt_is_planar);
    LOAD_SYMBOL(avutil, av_samples_alloc_array_and_samples);
    LOAD_SYMBOL(avutil, av_strdup);

    if (any_failed) {
        LOG_WARNING(Common, "Could not find all required functions in libavutil.");
        avutil.reset();
        return false;
    }

    LOG_INFO(Common, "Successfully loaded libavutil.");
    return true;
}

static bool LoadAVCodec() {
    if (avcodec) {
        return true;
    }

    avcodec = std::make_unique<Common::DynamicLibrary>("avcodec", LIBAVCODEC_VERSION_MAJOR);
    if (!avcodec->IsLoaded()) {
        LOG_WARNING(Common, "Could not dynamically load libavcodec: {}", avcodec->GetLoadError());
        avcodec.reset();
        return false;
    }

    auto any_failed = false;

    LOAD_SYMBOL(avcodec, avcodec_version);

    auto major_version = AV_VERSION_MAJOR(avcodec_version());
    if (major_version != LIBAVCODEC_VERSION_MAJOR) {
        LOG_WARNING(Common, "libavcodec version {} does not match supported version {}.",
                    major_version, LIBAVCODEC_VERSION_MAJOR);
        avcodec.reset();
        return false;
    }

    LOAD_SYMBOL(avcodec, av_codec_is_encoder);
    LOAD_SYMBOL(avcodec, av_codec_iterate);
    LOAD_SYMBOL(avcodec, av_init_packet);
    LOAD_SYMBOL(avcodec, av_packet_alloc);
    LOAD_SYMBOL(avcodec, av_packet_free);
    LOAD_SYMBOL(avcodec, av_packet_rescale_ts);
    LOAD_SYMBOL(avcodec, av_parser_close);
    LOAD_SYMBOL(avcodec, av_parser_init);
    LOAD_SYMBOL(avcodec, av_parser_parse2);
    LOAD_SYMBOL(avcodec, avcodec_alloc_context3);
    LOAD_SYMBOL(avcodec, avcodec_descriptor_next);
    LOAD_SYMBOL(avcodec, avcodec_find_decoder);
    LOAD_SYMBOL(avcodec, avcodec_find_encoder_by_name);
    LOAD_SYMBOL(avcodec, avcodec_free_context);
    LOAD_SYMBOL(avcodec, avcodec_get_class);
    LOAD_SYMBOL(avcodec, avcodec_get_hw_config);
    LOAD_SYMBOL(avcodec, avcodec_open2);
    LOAD_SYMBOL(avcodec, avcodec_parameters_from_context);
    LOAD_SYMBOL(avcodec, avcodec_receive_frame);
    LOAD_SYMBOL(avcodec, avcodec_receive_packet);
    LOAD_SYMBOL(avcodec, avcodec_send_frame);
    LOAD_SYMBOL(avcodec, avcodec_send_packet);

    if (any_failed) {
        LOG_WARNING(Common, "Could not find all required functions in libavcodec.");
        avcodec.reset();
        return false;
    }

    LOG_INFO(Common, "Successfully loaded libavcodec.");
    return true;
}

static bool LoadAVFilter() {
    if (avfilter) {
        return true;
    }

    avfilter = std::make_unique<Common::DynamicLibrary>("avfilter", LIBAVFILTER_VERSION_MAJOR);
    if (!avfilter->IsLoaded()) {
        LOG_WARNING(Common, "Could not dynamically load libavfilter: {}", avfilter->GetLoadError());
        avfilter.reset();
        return false;
    }

    auto any_failed = false;

    LOAD_SYMBOL(avfilter, avfilter_version);

    auto major_version = AV_VERSION_MAJOR(avfilter_version());
    if (major_version != LIBAVFILTER_VERSION_MAJOR) {
        LOG_WARNING(Common, "libavfilter version {} does not match supported version {}.",
                    major_version, LIBAVFILTER_VERSION_MAJOR);
        avfilter.reset();
        return false;
    }

    LOAD_SYMBOL(avfilter, av_buffersink_get_frame);
    LOAD_SYMBOL(avfilter, av_buffersrc_add_frame);
    LOAD_SYMBOL(avfilter, avfilter_get_by_name);
    LOAD_SYMBOL(avfilter, avfilter_graph_alloc);
    LOAD_SYMBOL(avfilter, avfilter_graph_config);
    LOAD_SYMBOL(avfilter, avfilter_graph_create_filter);
    LOAD_SYMBOL(avfilter, avfilter_graph_free);
    LOAD_SYMBOL(avfilter, avfilter_graph_parse_ptr);
    LOAD_SYMBOL(avfilter, avfilter_inout_alloc);
    LOAD_SYMBOL(avfilter, avfilter_inout_free);

    if (any_failed) {
        LOG_WARNING(Common, "Could not find all required functions in libavfilter.");
        avfilter.reset();
        return false;
    }

    LOG_INFO(Common, "Successfully loaded libavfilter.");
    return true;
}

static bool LoadAVFormat() {
    if (avformat) {
        return true;
    }

    avformat = std::make_unique<Common::DynamicLibrary>("avformat", LIBAVFORMAT_VERSION_MAJOR);
    if (!avformat->IsLoaded()) {
        LOG_WARNING(Common, "Could not dynamically load libavformat: {}", avformat->GetLoadError());
        avformat.reset();
        return false;
    }

    auto any_failed = false;

    LOAD_SYMBOL(avformat, avformat_version);

    auto major_version = AV_VERSION_MAJOR(avformat_version());
    if (major_version != LIBAVFORMAT_VERSION_MAJOR) {
        LOG_WARNING(Common, "libavformat version {} does not match supported version {}.",
                    major_version, LIBAVFORMAT_VERSION_MAJOR);
        avformat.reset();
        return false;
    }

    LOAD_SYMBOL(avformat, av_guess_format);
    LOAD_SYMBOL(avformat, av_interleaved_write_frame);
    LOAD_SYMBOL(avformat, av_muxer_iterate);
    LOAD_SYMBOL(avformat, av_write_trailer);
    LOAD_SYMBOL(avformat, avformat_alloc_output_context2);
    LOAD_SYMBOL(avformat, avformat_free_context);
    LOAD_SYMBOL(avformat, avformat_get_class);
    LOAD_SYMBOL(avformat, avformat_network_init);
    LOAD_SYMBOL(avformat, avformat_new_stream);
    LOAD_SYMBOL(avformat, avformat_query_codec);
    LOAD_SYMBOL(avformat, avformat_write_header);
    LOAD_SYMBOL(avformat, avio_closep);
    LOAD_SYMBOL(avformat, avio_open);

    if (any_failed) {
        LOG_WARNING(Common, "Could not find all required functions in libavformat.");
        avformat.reset();
        return false;
    }

    LOG_INFO(Common, "Successfully loaded libavformat.");
    return true;
}

static bool LoadSWResample() {
    if (swresample) {
        return true;
    }

    swresample =
        std::make_unique<Common::DynamicLibrary>("swresample", LIBSWRESAMPLE_VERSION_MAJOR);
    if (!swresample->IsLoaded()) {
        LOG_WARNING(Common, "Could not dynamically load libswresample: {}",
                    swresample->GetLoadError());
        swresample.reset();
        return false;
    }

    auto any_failed = false;

    LOAD_SYMBOL(swresample, swresample_version);

    auto major_version = AV_VERSION_MAJOR(swresample_version());
    if (major_version != LIBSWRESAMPLE_VERSION_MAJOR) {
        LOG_WARNING(Common, "libswresample version {} does not match supported version {}.",
                    major_version, LIBSWRESAMPLE_VERSION_MAJOR);
        swresample.reset();
        return false;
    }

#if LIBSWRESAMPLE_VERSION_INT >= AV_VERSION_INT(4, 5, 100)
    LOAD_SYMBOL(swresample, swr_alloc_set_opts2);
#else
    LOAD_SYMBOL(swresample, swr_alloc_set_opts);
#endif
    LOAD_SYMBOL(swresample, swr_convert);
    LOAD_SYMBOL(swresample, swr_free);
    LOAD_SYMBOL(swresample, swr_init);

    if (any_failed) {
        LOG_WARNING(Common, "Could not find all required functions in libswresample.");
        swresample.reset();
        return false;
    }

    LOG_INFO(Common, "Successfully loaded libswresample.");
    return true;
}

bool LoadFFmpeg() {
    return LoadAVUtil() && LoadAVCodec() && LoadAVFilter() && LoadAVFormat() && LoadSWResample();
}

} // namespace DynamicLibrary::FFmpeg