diff --git a/mingw-w64-ffmpeg4.4/0001-get_cabac_inline_x86-Dont-inline-if-32-bit-clang-on-windows.patch b/mingw-w64-ffmpeg4.4/0001-get_cabac_inline_x86-Dont-inline-if-32-bit-clang-on-windows.patch new file mode 100644 index 0000000000000..50c9832c973be --- /dev/null +++ b/mingw-w64-ffmpeg4.4/0001-get_cabac_inline_x86-Dont-inline-if-32-bit-clang-on-windows.patch @@ -0,0 +1,38 @@ +From 8990c5869e27fcd43b53045f87ba251f42e7d293 Mon Sep 17 00:00:00 2001 +From: Christopher Degawa +Date: Tue, 17 Aug 2021 10:35:39 -0500 +Subject: [PATCH] get_cabac_inline_x86: Don't inline if 32-bit clang on windows +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit + +Fixes https://trac.ffmpeg.org/ticket/8903 + +relevant https://github.com/msys2/MINGW-packages/discussions/9258 + +Signed-off-by: Christopher Degawa +Signed-off-by: Martin Storsjö +--- + libavcodec/x86/cabac.h | 9 +++++++-- + 1 file changed, 7 insertions(+), 2 deletions(-) + +diff --git a/libavcodec/x86/cabac.h b/libavcodec/x86/cabac.h +index 53d74c541e22..b046a56a6b7e 100644 +--- a/libavcodec/x86/cabac.h ++++ b/libavcodec/x86/cabac.h +@@ -177,8 +177,13 @@ + + #if HAVE_7REGS && !BROKEN_COMPILER + #define get_cabac_inline get_cabac_inline_x86 +-static av_always_inline int get_cabac_inline_x86(CABACContext *c, +- uint8_t *const state) ++static ++#if defined(_WIN32) && !defined(_WIN64) && defined(__clang__) ++av_noinline ++#else ++av_always_inline ++#endif ++int get_cabac_inline_x86(CABACContext *c, uint8_t *const state) + { + int bit, tmp; + #ifdef BROKEN_RELOCATIONS diff --git a/mingw-w64-ffmpeg4.4/0002-gcc-12.patch b/mingw-w64-ffmpeg4.4/0002-gcc-12.patch new file mode 100644 index 0000000000000..1ffdca0ae2002 --- /dev/null +++ b/mingw-w64-ffmpeg4.4/0002-gcc-12.patch @@ -0,0 +1,16 @@ +Building the assembler code in libavcodec/x86/cabac.h is broken for i386 +with GCC 12.1. +Remove this patch once this is fixed either in ffmpeg or in GCC. + +diff -urN ffmpeg-4.4.2/libavcodec/x86/cabac.h.orig ffmpeg-4.4.2/libavcodec/x86/cabac.h +--- ffmpeg-4.4.2/libavcodec/x86/cabac.h.orig 2022-05-22 15:09:32.786149500 +0200 ++++ ffmpeg-4.4.2/libavcodec/x86/cabac.h 2022-05-22 18:35:06.885673200 +0200 +@@ -29,7 +29,7 @@ + #include "libavutil/x86/asm.h" + #include "config.h" + +-#if (defined(__i386) && defined(__clang__) && (__clang_major__<2 || (__clang_major__==2 && __clang_minor__<10)))\ ++#if (defined(__i386) && (!defined(__clang__) || (defined(__clang__) && (__clang_major__<2 || (__clang_major__==2 && __clang_minor__<10)))))\ + || ( !defined(__clang__) && defined(__llvm__) && __GNUC__==4 && __GNUC_MINOR__==2 && __GNUC_PATCHLEVEL__<=1)\ + || (defined(__INTEL_COMPILER) && defined(_MSC_VER)) + # define BROKEN_COMPILER 1 diff --git a/mingw-w64-ffmpeg4.4/0003-fix-clang32-build.patch b/mingw-w64-ffmpeg4.4/0003-fix-clang32-build.patch new file mode 100644 index 0000000000000..eb32eceb48f27 --- /dev/null +++ b/mingw-w64-ffmpeg4.4/0003-fix-clang32-build.patch @@ -0,0 +1,11 @@ +--- ffmpeg-4.4.3/libavutil/hwcontext_vulkan.c.orig 2022-10-21 07:43:58.074260200 +0200 ++++ ffmpeg-4.4.3/libavutil/hwcontext_vulkan.c 2022-10-21 07:44:11.546170700 +0200 +@@ -862,7 +862,7 @@ + + av_freep(&cmd->queues); + av_freep(&cmd->bufs); +- cmd->pool = NULL; ++ cmd->pool = VK_NULL_HANDLE; + } + + static VkCommandBuffer get_buf_exec_ctx(AVHWFramesContext *hwfc, VulkanExecCtx *cmd) diff --git a/mingw-w64-ffmpeg4.4/PKGBUILD b/mingw-w64-ffmpeg4.4/PKGBUILD new file mode 100644 index 0000000000000..2e27e9d215a03 --- /dev/null +++ b/mingw-w64-ffmpeg4.4/PKGBUILD @@ -0,0 +1,237 @@ +# Maintainer: Alexey Pavlov +# Contributor: Zach Bacon <11doctorwhocanada@gmail.com> +# Contributor: wirx6 +# Contributor: Ray Donnelly + +_realname=ffmpeg +pkgbase="mingw-w64-${_realname}4.4" +pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}4.4" +pkgver=4.4.3 +pkgrel=1 +pkgdesc="Complete and free Internet live audio and video broadcasting solution (mingw-w64)" +arch=('any') +mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64') +url="https://ffmpeg.org/" +license=('spdx:GPL-3.0-or-later') +options=('staticlibs' 'strip') +depends=("${MINGW_PACKAGE_PREFIX}-aom" + "${MINGW_PACKAGE_PREFIX}-bzip2" + # "${MINGW_PACKAGE_PREFIX}-celt" # provided by opus, not in development + "${MINGW_PACKAGE_PREFIX}-fribidi" + "${MINGW_PACKAGE_PREFIX}-fontconfig" + "${MINGW_PACKAGE_PREFIX}-dav1d" + "${MINGW_PACKAGE_PREFIX}-gmp" + "${MINGW_PACKAGE_PREFIX}-gnutls" + "${MINGW_PACKAGE_PREFIX}-gsm" + "${MINGW_PACKAGE_PREFIX}-lame" + "${MINGW_PACKAGE_PREFIX}-libass" + "${MINGW_PACKAGE_PREFIX}-libbluray" + "${MINGW_PACKAGE_PREFIX}-libcaca" + "${MINGW_PACKAGE_PREFIX}-libexif" + "${MINGW_PACKAGE_PREFIX}-libgme" + "${MINGW_PACKAGE_PREFIX}-libiconv" + $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] || echo "${MINGW_PACKAGE_PREFIX}-libmfx") + "${MINGW_PACKAGE_PREFIX}-libmodplug" + "${MINGW_PACKAGE_PREFIX}-libssh" + "${MINGW_PACKAGE_PREFIX}-librsvg" + "${MINGW_PACKAGE_PREFIX}-libsoxr" + "${MINGW_PACKAGE_PREFIX}-libtheora" + "${MINGW_PACKAGE_PREFIX}-libvorbis" + "${MINGW_PACKAGE_PREFIX}-libvpx" + "${MINGW_PACKAGE_PREFIX}-libwebp" + "${MINGW_PACKAGE_PREFIX}-libxml2" + "${MINGW_PACKAGE_PREFIX}-openal" + "${MINGW_PACKAGE_PREFIX}-opencore-amr" + "${MINGW_PACKAGE_PREFIX}-openjpeg2" + "${MINGW_PACKAGE_PREFIX}-opus" + $([[ "${MINGW_PACKAGE_PREFIX}" == *clang-aarch64* ]] \ + || echo "${MINGW_PACKAGE_PREFIX}-rav1e") + "${MINGW_PACKAGE_PREFIX}-rtmpdump" + "${MINGW_PACKAGE_PREFIX}-SDL2" + "${MINGW_PACKAGE_PREFIX}-speex" + "${MINGW_PACKAGE_PREFIX}-srt" + $([[ "${CARCH}" != "x86_64" ]] || echo "${MINGW_PACKAGE_PREFIX}-svt-av1") + "${MINGW_PACKAGE_PREFIX}-vid.stab" + "${MINGW_PACKAGE_PREFIX}-vulkan" + "${MINGW_PACKAGE_PREFIX}-libx264" + "${MINGW_PACKAGE_PREFIX}-x265" + "${MINGW_PACKAGE_PREFIX}-xvidcore" + "${MINGW_PACKAGE_PREFIX}-zimg" + "${MINGW_PACKAGE_PREFIX}-zlib") +makedepends=("${MINGW_PACKAGE_PREFIX}-cc" + "${MINGW_PACKAGE_PREFIX}-autotools" + "${MINGW_PACKAGE_PREFIX}-pkgconf" + "${MINGW_PACKAGE_PREFIX}-dlfcn" + $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] || echo "${MINGW_PACKAGE_PREFIX}-amf-headers") + $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] || echo "${MINGW_PACKAGE_PREFIX}-ffnvcodec-headers") + $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] || echo "${MINGW_PACKAGE_PREFIX}-nasm")) +source=(https://ffmpeg.org/releases/${_realname}-${pkgver}.tar.xz{,.asc} + https://github.com/FFmpeg/FFmpeg/commit/c6fdbe26ef30fff817581e5ed6e078d96111248a.patch + "pathtools.c" + "pathtools.h" + # Backport from https://github.com/FFmpeg/FFmpeg/commit/8990c5869e27fcd43b53045f87ba251f42e7d293 + "0001-get_cabac_inline_x86-Dont-inline-if-32-bit-clang-on-windows.patch" + "0002-gcc-12.patch" + "0003-fix-clang32-build.patch") +validpgpkeys=('FCF986EA15E6E293A5644F10B4322F04D67658D8') +sha256sums=('6c5b6c195e61534766a0b5fe16acc919170c883362612816d0a1c7f4f947006e' + 'SKIP' + '8de70945eeb6a998b9188494a242ba06847b475b9674fcfec48436d99fcbea7f' + '703cd0cb74e714f9e66d26de11c109dd76fab07e723af8dde56a35ea65102e5f' + '4f9d325265ef6f4e90ad637dea41afa6995388c921fe961ad5dc895aca10318b' + 'b19edf64c3daff6acebeb2e45be4e13c5ee035fd24ba1bf9e77a5376efe83628' + '84b9fcaa188eef15201a105a44c53d647e4fb800a5032336e0948d6bed2cbc1b' + '06481611d3449e2cc4f4759d2e0ad5b1ce2c4f313d0b63d4cb0a51d9fe02a424') + +# Helper macros to help make tasks easier # +apply_patch_with_msg() { + for _fname in "$@" + do + msg2 "Applying ${_fname}" + patch -Nbp1 -i "${srcdir}"/${_fname} + done +} + +prepare() { + test ! -d "${startdir}/../mingw-w64-pathtools" || { + cmp "${startdir}/../mingw-w64-pathtools/pathtools.c" "${srcdir}/pathtools.c" && + cmp "${startdir}/../mingw-w64-pathtools/pathtools.h" "${srcdir}/pathtools.h" + } || exit 1 + + cd "${srcdir}/${_realname}-${pkgver}" + cp -fHv "${srcdir}"/pathtools.[ch] libavfilter/ + + apply_patch_with_msg \ + c6fdbe26ef30fff817581e5ed6e078d96111248a.patch \ + 0001-get_cabac_inline_x86-Dont-inline-if-32-bit-clang-on-windows.patch \ + 0002-gcc-12.patch \ + 0003-fix-clang32-build.patch +} + +build() { + # Fix using SRT headers + CFLAGS+=" -DWIN32" + CXXFLAGS+=" -DWIN32" + + local -a common_config + common_config+=( + --incdir="${MINGW_PREFIX}/include/ffmpeg4.4" \ + --libdir="${MINGW_PREFIX}/lib/ffmpeg4.4" \ + --disable-debug + --enable-dxva2 + --enable-d3d11va + --enable-fontconfig + --enable-gmp + --enable-gnutls + --enable-gpl + --enable-iconv + --enable-libaom + --enable-libass + --enable-libbluray + --enable-libcaca + --enable-libdav1d + --enable-libfreetype + --enable-libfribidi + --enable-libgme + --enable-libgsm + --enable-libmodplug + --enable-libmp3lame + --enable-libopencore_amrnb + --enable-libopencore_amrwb + --enable-libopenjpeg + --enable-libopus + --enable-librsvg + --enable-librtmp + --enable-libssh + --enable-libsoxr + --enable-libspeex + --enable-libsrt + --enable-libtheora + --enable-libvidstab + --enable-libvorbis + --enable-libx264 + --enable-libx265 + --enable-libxvid + --enable-libvpx + --enable-libwebp + --enable-libxml2 + --enable-libzimg + --enable-openal + --enable-pic + --enable-postproc + --enable-runtime-cpudetect + --enable-swresample + --enable-version3 + --enable-vulkan + --enable-zlib + --disable-doc + ) + + if [[ "${CARCH}" == "x86_64" ]]; then + common_config+=( + --enable-libsvtav1 + ) + fi + + if [[ "${MINGW_PACKAGE_PREFIX}" != *clang-aarch64* ]]; then + common_config+=( + --enable-librav1e + ) + fi + + if [[ "${MINGW_PACKAGE_PREFIX}" != *clang-aarch64* ]]; then + common_config+=( + --enable-libmfx + --enable-amf + --enable-nvenc + ) + fi + + for _variant in -static -shared; do + [[ -d "${srcdir}/build-${MSYSTEM}${_variant}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}${_variant}" + mkdir -p "${srcdir}/build-${MSYSTEM}${_variant}" && cd "${srcdir}/build-${MSYSTEM}${_variant}" + if [[ ${_variant} == -static ]]; then + ENABLE_VARIANT="--enable-static --pkg-config-flags=--static" + else + ENABLE_VARIANT=--enable-shared + fi + ../${_realname}-${pkgver}/configure \ + --prefix=${MINGW_PREFIX} \ + --target-os=mingw32 \ + --arch=${CARCH%%-*} \ + --cc=${CC} \ + --cxx=${CXX} \ + "${common_config[@]}" \ + --logfile=config.log \ + ${ENABLE_VARIANT} || + { + cat config.log + exit 1 + } + make VERBOSE=1 + done +} + +check() { + for _variant in -static -shared; do + cd "${srcdir}/build-${MSYSTEM}${_variant}" + # workaround for conflict with SDL main(), use it if you have SDL installed + # make check CC_C="-c -Umain" + make check VERBOSE=1 || true + done +} + +package() { + for _variant in -static -shared; do + cd "${srcdir}/build-${MSYSTEM}${_variant}" + make DESTDIR="${pkgdir}" install + done + + rm -f ${pkgdir}/${MINGW_PREFIX}/lib/ffmpeg4.4/*.def + rm -f ${pkgdir}/${MINGW_PREFIX}/bin/*.lib + rm -f ${pkgdir}/${MINGW_PREFIX}/bin/*.exe + rm -rf ${pkgdir}/${MINGW_PREFIX}/share + + local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX}) + find ${pkgdir}${MINGW_PREFIX}/lib/ffmpeg4.4/pkgconfig -name *.pc -exec sed -i -e"s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" {} \; +} diff --git a/mingw-w64-ffmpeg4.4/c6fdbe26ef30fff817581e5ed6e078d96111248a.patch b/mingw-w64-ffmpeg4.4/c6fdbe26ef30fff817581e5ed6e078d96111248a.patch new file mode 100644 index 0000000000000..801474155757a --- /dev/null +++ b/mingw-w64-ffmpeg4.4/c6fdbe26ef30fff817581e5ed6e078d96111248a.patch @@ -0,0 +1,28 @@ +From c6fdbe26ef30fff817581e5ed6e078d96111248a Mon Sep 17 00:00:00 2001 +From: dvhh +Date: Sat, 18 Jun 2022 01:46:12 +0900 +Subject: [PATCH] configure: fix SDL2 version check for pkg_config fallback + +pkg_config fallback for SDL2 use 2.1.0 as max (excluded) version +where the pkg_config specify 3.0.0 +Correcting fallback version to be in line with the pkg_config version + +Signed-off-by: dvhh +Signed-off-by: Marton Balint +--- + configure | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/configure b/configure +index 7d5c4900bf8c..0558e937ca75 100755 +--- a/configure ++++ b/configure +@@ -6756,7 +6756,7 @@ if enabled sdl2; then + sdl2_cflags=$("${SDL2_CONFIG}" --cflags) + sdl2_extralibs=$("${SDL2_CONFIG}" --libs) + test_cpp_condition SDL.h "(SDL_MAJOR_VERSION<<16 | SDL_MINOR_VERSION<<8 | SDL_PATCHLEVEL) >= 0x020001" $sdl2_cflags && +- test_cpp_condition SDL.h "(SDL_MAJOR_VERSION<<16 | SDL_MINOR_VERSION<<8 | SDL_PATCHLEVEL) < 0x020100" $sdl2_cflags && ++ test_cpp_condition SDL.h "(SDL_MAJOR_VERSION<<16 | SDL_MINOR_VERSION<<8 | SDL_PATCHLEVEL) < 0x030000" $sdl2_cflags && + check_func_headers SDL_events.h SDL_PollEvent $sdl2_extralibs $sdl2_cflags && + enable sdl2 + fi diff --git a/mingw-w64-ffmpeg4.4/pathtools.c b/mingw-w64-ffmpeg4.4/pathtools.c new file mode 100644 index 0000000000000..d8d509be468bf --- /dev/null +++ b/mingw-w64-ffmpeg4.4/pathtools.c @@ -0,0 +1,636 @@ +/* + .Some useful path tools. + .ASCII only for now. + .Written by Ray Donnelly in 2014. + .Licensed under CC0 (and anything. + .else you need to license it under). + .No warranties whatsoever. + .email: . + */ + +#if defined(__APPLE__) +#include +#else +#include +#endif +#include +#include +#include +#if defined(__linux__) || defined(__CYGWIN__) || defined(__MSYS__) +#include +#endif +#include + +/* If you don't define this, then get_executable_path() + can only use argv[0] which will often not work well */ +#define IMPLEMENT_SYS_GET_EXECUTABLE_PATH + +#if defined(IMPLEMENT_SYS_GET_EXECUTABLE_PATH) +#if defined(__linux__) || defined(__CYGWIN__) || defined(__MSYS__) +/* Nothing needed, unistd.h is enough. */ +#elif defined(__APPLE__) +#include +#elif defined(_WIN32) +#define WIN32_MEAN_AND_LEAN +#include +#include +#endif +#endif /* defined(IMPLEMENT_SYS_GET_EXECUTABLE_PATH) */ + +#include "pathtools.h" + +char * +malloc_copy_string(char const * original) +{ + char * result = (char *) malloc (sizeof (char*) * strlen (original)+1); + if (result != NULL) + { + strcpy (result, original); + } + return result; +} + +void +sanitise_path(char * path) +{ + size_t path_size = strlen (path); + + /* Replace any '\' with '/' */ + char * path_p = path; + while ((path_p = strchr (path_p, '\\')) != NULL) + { + *path_p = '/'; + } + /* Replace any '//' with '/' */ + path_p = path + !!*path; /* skip first character, if any, to handle UNC paths correctly */ + while ((path_p = strstr (path_p, "//")) != NULL) + { + memmove (path_p, path_p + 1, path_size--); + } + return; +} + +char * +get_relative_path(char const * from_in, char const * to_in) +{ + size_t from_size = (from_in == NULL) ? 0 : strlen (from_in); + size_t to_size = (to_in == NULL) ? 0 : strlen (to_in); + size_t max_size = (from_size + to_size) * 2 + 4; + char * scratch_space = (char *) alloca (from_size + 1 + to_size + 1 + max_size + max_size); + char * from; + char * to; + char * common_part; + char * result; + size_t count; + + /* No to, return "./" */ + if (to_in == NULL) + { + return malloc_copy_string ("./"); + } + + /* If alloca failed or no from was given return a copy of to */ + if ( from_in == NULL + || scratch_space == NULL ) + { + return malloc_copy_string (to_in); + } + + from = scratch_space; + strcpy (from, from_in); + to = from + from_size + 1; + strcpy (to, to_in); + common_part = to + to_size + 1; + result = common_part + max_size; + simplify_path (from); + simplify_path (to); + + result[0] = '\0'; + + size_t match_size_dirsep = 0; /* The match size up to the last /. Always wind back to this - 1 */ + size_t match_size = 0; /* The running (and final) match size. */ + size_t largest_size = (from_size > to_size) ? from_size : to_size; + int to_final_is_slash = (to[to_size-1] == '/') ? 1 : 0; + char from_c; + char to_c; + for (match_size = 0; match_size < largest_size; ++match_size) + { + /* To simplify the logic, always pretend the strings end with '/' */ + from_c = (match_size < from_size) ? from[match_size] : '/'; + to_c = (match_size < to_size) ? to[match_size] : '/'; + + if (from_c != to_c) + { + if (from_c != '\0' || to_c != '\0') + { + match_size = match_size_dirsep; + } + break; + } + else if (from_c == '/') + { + match_size_dirsep = match_size; + } + } + strncpy (common_part, from, match_size); + common_part[match_size] = '\0'; + from += match_size; + to += match_size; + size_t ndotdots = 0; + char const* from_last = from + strlen(from) - 1; + while ((from = strchr (from, '/')) && from != from_last) + { + ++ndotdots; + ++from; + } + for (count = 0; count < ndotdots; ++count) + { + strcat(result, "../"); + } + if (strlen(to) > 0) + { + strcat(result, to+1); + } + /* Make sure that if to ends with '/' result does the same, and + vice-versa. */ + size_t size_result = strlen(result); + if ((to_final_is_slash == 1) + && (!size_result || result[size_result-1] != '/')) + { + strcat (result, "/"); + } + else if (!to_final_is_slash + && size_result && result[size_result-1] == '/') + { + result[size_result-1] = '\0'; + } + + return malloc_copy_string (result); +} + +void +simplify_path(char * path) +{ + ssize_t n_toks = 1; /* in-case we need an empty initial token. */ + ssize_t i, j; + size_t tok_size; + size_t in_size = strlen (path); + int it_ended_with_a_slash = (path[in_size - 1] == '/') ? 1 : 0; + char * result = path; + if (path[0] == '/' && path[1] == '/') { + /* preserve UNC path */ + path++; + in_size--; + result++; + } + sanitise_path(result); + char * result_p = result; + + do + { + ++n_toks; + ++result_p; + } while ((result_p = strchr (result_p, '/')) != NULL); + + result_p = result; + char const ** toks = (char const **) alloca (sizeof (char const*) * n_toks); + n_toks = 0; + do + { + if (result_p > result) + { + *result_p++ = '\0'; + } + else if (*result_p == '/') + { + /* A leading / creates an empty initial token. */ + toks[n_toks++] = result_p; + *result_p++ = '\0'; + } + toks[n_toks++] = result_p; + } while ((result_p = strchr (result_p, '/')) != NULL); + + /* Remove all non-leading '.' and any '..' we can match + with an earlier forward path (i.e. neither '.' nor '..') */ + for (i = 1; i < n_toks; ++i) + { + int removals[2] = { -1, -1 }; + if ( strcmp (toks[i], "." ) == 0) + { + removals[0] = i; + } + else if ( strcmp (toks[i], ".." ) == 0) + { + /* Search backwards for a forward path to collapse. + If none are found then the .. also stays. */ + for (j = i - 1; j > -1; --j) + { + if ( strcmp (toks[j], "." ) + && strcmp (toks[j], ".." ) ) + { + removals[0] = j; + removals[1] = i; + break; + } + } + } + for (j = 0; j < 2; ++j) + { + if (removals[j] >= 0) /* Can become -2 */ + { + --n_toks; + memmove (&toks[removals[j]], &toks[removals[j]+1], (n_toks - removals[j])*sizeof (char*)); + --i; + if (!j) + { + --removals[1]; + } + } + } + } + result_p = result; + for (i = 0; i < n_toks; ++i) + { + tok_size = strlen(toks[i]); + memmove (result_p, toks[i], tok_size); + result_p += tok_size; + if ((!i || tok_size) && ((i < n_toks - 1) || it_ended_with_a_slash == 1)) + { + *result_p = '/'; + ++result_p; + } + } + *result_p = '\0'; +} + +/* Returns actual_to by calculating the relative path from -> to and + applying that to actual_from. An assumption that actual_from is a + dir is made, and it may or may not end with a '/' */ +char const * +get_relocated_path (char const * from, char const * to, char const * actual_from) +{ + char const * relative_from_to = get_relative_path (from, to); + char * actual_to = (char *) malloc (strlen(actual_from) + 2 + strlen(relative_from_to)); + return actual_to; +} + +int +get_executable_path(char const * argv0, char * result, ssize_t max_size) +{ + char * system_result = (char *) alloca (max_size); + ssize_t system_result_size = -1; + ssize_t result_size = -1; + + if (system_result != NULL) + { +#if defined(IMPLEMENT_SYS_GET_EXECUTABLE_PATH) +#if defined(__linux__) || defined(__CYGWIN__) || defined(__MSYS__) + system_result_size = readlink("/proc/self/exe", system_result, max_size); +#elif defined(__APPLE__) + uint32_t bufsize = (uint32_t)max_size; + if (_NSGetExecutablePath(system_result, &bufsize) == 0) + { + system_result_size = (ssize_t)bufsize; + } +#elif defined(_WIN32) + unsigned long bufsize = (unsigned long)max_size; + system_result_size = GetModuleFileNameA(NULL, system_result, bufsize); + if (system_result_size == 0 || system_result_size == (ssize_t)bufsize) + { + /* Error, possibly not enough space. */ + system_result_size = -1; + } + else + { + /* Early conversion to unix slashes instead of more changes + everywhere else .. */ + char * winslash; + system_result[system_result_size] = '\0'; + while ((winslash = strchr (system_result, '\\')) != NULL) + { + *winslash = '/'; + } + } +#else +#warning "Don't know how to get executable path on this system" +#endif +#endif /* defined(IMPLEMENT_SYS_GET_EXECUTABLE_PATH) */ + } + /* Use argv0 as a default in-case of failure */ + if (system_result_size != -1) + { + strncpy (result, system_result, system_result_size); + result[system_result_size] = '\0'; + } + else + { + if (argv0 != NULL) + { + strncpy (result, argv0, max_size); + result[max_size-1] = '\0'; + } + else + { + result[0] = '\0'; + } + } + result_size = strlen (result); + return result_size; +} + +#if defined(_WIN32) +int +get_dll_path(char * result, unsigned long max_size) +{ + HMODULE handle; + char * p; + int ret; + + if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | + GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + (LPCSTR) &get_dll_path, &handle)) + { + return -1; + } + + ret = GetModuleFileNameA(handle, result, max_size); + if (ret == 0 || ret == (int)max_size) + { + return -1; + } + + /* Early conversion to unix slashes instead of more changes + everywhere else .. */ + result[ret] = '\0'; + p = result - 1; + while ((p = strchr (p + 1, '\\')) != NULL) + { + *p = '/'; + } + + return ret; +} +#endif + +char const * +strip_n_prefix_folders(char const * path, size_t n) +{ + if (path == NULL) + { + return NULL; + } + + if (path[0] != '/') + { + return path; + } + + char const * last = path; + while (n-- && path != NULL) + { + last = path; + path = strchr (path + 1, '/'); + } + return (path == NULL) ? last : path; +} + +void +strip_n_suffix_folders(char * path, size_t n) +{ + if (path == NULL) + { + return; + } + while (n--) + { + if (strrchr (path + 1, '/')) + { + *strrchr (path + 1, '/') = '\0'; + } + else + { + return; + } + } + return; +} + +size_t +split_path_list(char const * path_list, char split_char, char *** arr) +{ + size_t path_count; + size_t path_list_size; + char const * path_list_p; + + path_list_p = path_list; + if (path_list == NULL || path_list[0] == '\0') + { + return 0; + } + path_list_size = strlen (path_list); + + path_count = 0; + do + { + ++path_count; + ++path_list_p; + } + while ((path_list_p = strchr (path_list_p, split_char)) != NULL); + + /* allocate everything in one go. */ + char * all_memory = (char *) malloc (sizeof (char *) * path_count + strlen(path_list) + 1); + if (all_memory == NULL) + return 0; + *arr = (char **)all_memory; + all_memory += sizeof (char *) * path_count; + + path_count = 0; + path_list_p = path_list; + char const * next_path_list_p = 0; + do + { + next_path_list_p = strchr (path_list_p, split_char); + if (next_path_list_p != NULL) + { + ++next_path_list_p; + } + size_t this_size = (next_path_list_p != NULL) + ? next_path_list_p - path_list_p - 1 + : &path_list[path_list_size] - path_list_p; + memcpy (all_memory, path_list_p, this_size); + all_memory[this_size] = '\0'; + (*arr)[path_count++] = all_memory; + all_memory += this_size + 1; + } while ((path_list_p = next_path_list_p) != NULL); + + return path_count; +} + +static char * +get_relocated_path_list_ref(char const * from, char const * to_path_list, char *ref_path) +{ + char * temp; + if ((temp = strrchr (ref_path, '/')) != NULL) + { + temp[1] = '\0'; + } + + char **arr = NULL; + /* Ask Alexey why he added this. Are we not 100% sure + that we're dealing with unix paths here? */ + char split_char = ':'; + if (strchr (to_path_list, ';')) + { + split_char = ';'; + } + size_t count = split_path_list (to_path_list, split_char, &arr); + int result_size = 1 + (count - 1); /* count - 1 is for ; delim. */ + size_t ref_path_size = strlen (ref_path); + size_t i; + /* Space required is: + count * (ref_path_size + strlen (rel_to_datadir)) + rel_to_datadir upper bound is: + (count * strlen (from)) + (3 * num_slashes (from)) + + strlen(arr[i]) + 1. + .. pathalogically num_slashes (from) is strlen (from) + (from = ////////) */ + size_t space_required = (count * (ref_path_size + 4 * strlen (from))) + count - 1; + for (i = 0; i < count; ++i) + { + space_required += strlen (arr[i]); + } + char * scratch = (char *) alloca (space_required); + if (scratch == NULL) + return NULL; + for (i = 0; i < count; ++i) + { + char * rel_to_datadir = get_relative_path (from, arr[i]); + scratch[0] = '\0'; + arr[i] = scratch; + strcat (scratch, ref_path); + strcat (scratch, rel_to_datadir); + simplify_path (arr[i]); + size_t arr_i_size = strlen (arr[i]); + result_size += arr_i_size; + scratch = arr[i] + arr_i_size + 1; + } + char * result = (char *) malloc (result_size); + if (result == NULL) + { + return NULL; + } + result[0] = '\0'; + for (i = 0; i < count; ++i) + { + strcat (result, arr[i]); + if (i != count-1) + { +#if defined(_WIN32) + strcat (result, ";"); +#else + strcat (result, ":"); +#endif + } + } + free ((void*)arr); + return result; +} + +char * +get_relocated_path_list(char const *from, char const *to_path_list) +{ + char exe_path[MAX_PATH]; + get_executable_path (NULL, &exe_path[0], sizeof (exe_path) / sizeof (exe_path[0])); + + return get_relocated_path_list_ref(from, to_path_list, exe_path); +} + +char * +get_relocated_path_list_lib(char const *from, char const *to_path_list) +{ + char dll_path[PATH_MAX]; + get_dll_path (&dll_path[0], sizeof(dll_path)/sizeof(dll_path[0])); + + return get_relocated_path_list_ref(from, to_path_list, dll_path); +} + +static char * +single_path_relocation_ref(const char *from, const char *to, char *ref_path) +{ +#if defined(__MINGW32__) + if (strrchr (ref_path, '/') != NULL) + { + strrchr (ref_path, '/')[1] = '\0'; + } + char * rel_to_datadir = get_relative_path (from, to); + strcat (ref_path, rel_to_datadir); + simplify_path (&ref_path[0]); + return malloc_copy_string(ref_path); +#else + return malloc_copy_string(to); +#endif +} + +char * +single_path_relocation(const char *from, const char *to) +{ +#if defined(__MINGW32__) + char exe_path[PATH_MAX]; + get_executable_path (NULL, &exe_path[0], sizeof(exe_path)/sizeof(exe_path[0])); + return single_path_relocation_ref(from, to, exe_path); +#else + return malloc_copy_string(to); +#endif +} + +char * +single_path_relocation_lib(const char *from, const char *to) +{ +#if defined(__MINGW32__) + char dll_path[PATH_MAX]; + get_dll_path (&dll_path[0], sizeof(dll_path)/sizeof(dll_path[0])); + return single_path_relocation_ref(from, to, dll_path); +#else + return malloc_copy_string(to); +#endif +} + +char * +pathlist_relocation(const char *from_path, const char *to_path_list) +{ +#if defined(__MINGW32__) + static char stored_path[PATH_MAX]; + static int stored = 0; + if (stored == 0) + { + char const * relocated = get_relocated_path_list(from_path, to_path_list); + strncpy (stored_path, relocated, PATH_MAX); + stored_path[PATH_MAX-1] = '\0'; + free ((void *)relocated); + stored = 1; + } + return stored_path; +#else + return (to_path_list); +#endif +} + +char * +pathlist_relocation_lib(const char *from_path, const char *to_path_list) +{ +#if defined(__MINGW32__) + static char stored_path[PATH_MAX]; + static int stored = 0; + if (stored == 0) + { + char const * relocated = get_relocated_path_list_lib(from_path, to_path_list); + strncpy (stored_path, relocated, PATH_MAX); + stored_path[PATH_MAX-1] = '\0'; + free ((void *)relocated); + stored = 1; + } + return stored_path; +#else + return (to_path_list); +#endif +} diff --git a/mingw-w64-ffmpeg4.4/pathtools.h b/mingw-w64-ffmpeg4.4/pathtools.h new file mode 100644 index 0000000000000..08423a999e294 --- /dev/null +++ b/mingw-w64-ffmpeg4.4/pathtools.h @@ -0,0 +1,60 @@ +/* + .Some useful path tools. + .ASCII only for now. + .Written by Ray Donnelly in 2014. + .Licensed under CC0 (and anything. + .else you need to license it under). + .No warranties whatsoever. + .email: . + */ + +#ifndef PATHTOOLS_H +#define PATHTOOLS_H + +#include +#if defined(__APPLE__) +#include +#else +#include +#endif +#include + +char * malloc_copy_string(char const * original); + +/* In-place replaces any '\' with '/' and any '//' with '/' */ +void sanitise_path(char * path); + +/* Uses a host OS specific function to determine the path of the executable, + if IMPLEMENT_SYS_GET_EXECUTABLE_PATH is defined, otherwise uses argv0. */ +int get_executable_path(char const * argv0, char * result, ssize_t max_size); + +#if defined(_WIN32) +int get_dll_path(char * result, unsigned long max_size); +#endif + +/* Where possible, in-place removes occourances of '.' and 'path/..' */ +void simplify_path(char * path); + +/* Allocates (via malloc) and returns the path to get from from to to. */ +char * get_relative_path(char const * from, char const * to); + +size_t split_path_list(char const * path_list, char split_char, char *** arr); + +/* Advances path along by the amount that removes n prefix folders. */ +char const * +strip_n_prefix_folders(char const * path, size_t n); + +/* NULL terminates path to remove n suffix folders. */ +void +strip_n_suffix_folders(char * path, size_t n); + +char const * get_relocated_path (char const * from, char const * to, char const * actual_from); +char * get_relocated_path_list(char const * from, char const * to_path_list); +char * get_relocated_path_list_lib(char const * from, char const * to_path_list); + +char * single_path_relocation(const char *from, const char *to); +char * single_path_relocation_lib(const char *from, const char *to); +char * pathlist_relocation(const char *from_path, const char *to_path_list); +char * pathlist_relocation_lib(const char *from_path, const char *to_path_list); + +#endif /* PATHTOOLS_H */