1 
2 #include "libs/ffmpeg/FFmpeg.h"
3 
4 #include "FFmpegHeaders.h"
5 
6 namespace {
7 const int MIN_LOG_LEVEL = AV_LOG_WARNING;
8 
9 bool initialized = false;
10 
11 #ifndef NDEBUG
log_callback_report(void * ptr,int level,const char * fmt,va_list vl)12 void log_callback_report(void* ptr, int level, const char* fmt, va_list vl) {
13 	if (level > MIN_LOG_LEVEL) {
14 		return;
15 	}
16 
17 	char buffer[1024];
18 	int print_prefix = 1;
19 	av_log_format_line(ptr, level, fmt, vl, buffer, sizeof(buffer), &print_prefix);
20 
21 	mprintf(("FFMPEG Log: %s", buffer)); // no \n, ffmpeg handles that
22 }
23 #endif
24 
check_version(const char * libname,uint32_t current,uint32_t compiled)25 void check_version(const char* libname, uint32_t current, uint32_t compiled)
26 {
27 	mprintf(("FFmpeg: Using %s with version %d.%d.%d. Compiled with version %d.%d.%d\n", libname,
28 		AV_VERSION_MAJOR(current), AV_VERSION_MINOR(current), AV_VERSION_MICRO(current),
29 		AV_VERSION_MAJOR(compiled), AV_VERSION_MINOR(compiled), AV_VERSION_MICRO(compiled)));
30 
31 	auto current_major = AV_VERSION_MAJOR(current);
32 	auto current_minor = AV_VERSION_MINOR(current);
33 
34 	auto compiled_major = AV_VERSION_MAJOR(compiled);
35 	auto compiled_minor = AV_VERSION_MINOR(compiled);
36 
37 	if (current_major != compiled_major)
38 	{
39 		Error(LOCATION, "The major version of the %s library is not the same as the one this executable was compiled with!\n"
40 			"Current major version is %" PRIu32 " but this executable was compiled with major version %" PRIu32 ".\n"
41 			"This may be caused by using outdated DLLs, if you downloaded these builds then try reextracting the zip file.", libname, current_major, compiled_major);
42 	}
43 
44 	if (current_minor < compiled_minor)
45 	{
46 		Error(LOCATION, "The minor version of the %s library is not the same as the one this executable was compiled with!\n"
47 			"Current minor version is %" PRIu32 " but this executable was compiled with minor version %" PRIu32 ".\n"
48 			"This may be caused by using outdated DLLs, if you downloaded these builds then try reextracting the zip file.", libname, current_minor, compiled_minor);
49 	}
50 }
51 }
52 
53 namespace libs {
54 namespace ffmpeg {
initialize()55 void initialize() {
56 	if (initialized) {
57 		return;
58 	}
59 
60 	// This is deprecated since 58.9.100 and not needed anymore
61 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
62 	av_register_all();
63 #endif
64 
65 	check_version("libavcodec", avcodec_version(), LIBAVCODEC_VERSION_INT);
66 	check_version("libavformat", avformat_version(), LIBAVFORMAT_VERSION_INT);
67 	check_version("libavutil", avutil_version(), LIBAVUTIL_VERSION_INT);
68 	check_version("libswresample", swresample_version(), LIBSWRESAMPLE_VERSION_INT);
69 	check_version("libswscale", swscale_version(), LIBSWSCALE_VERSION_INT);
70 
71 #ifndef NDEBUG
72 	av_log_set_callback(&log_callback_report);
73 	av_log_set_level(MIN_LOG_LEVEL);
74 #else
75 	av_log_set_level(AV_LOG_QUIET);
76 #endif
77 
78 	mprintf(("FFmpeg library initialized!\n"));
79 	mprintf(("FFmpeg: License: %s\n", avformat_license()));
80 
81 	initialized = true;
82 }
83 }
84 }
85