# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Zach Bacon <11doctorwhocanada@gmail.com>
# Contributor: wirx6 <wirx654@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=ffmpeg
pkgbase="mingw-w64-${_realname}4.4"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}4.4"
pkgver=4.4.6
pkgrel=7
pkgdesc="Complete solution to record, convert and stream audio and video (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://ffmpeg.org/"
msys2_repository_url="https://git.ffmpeg.org/gitweb/ffmpeg.git/shortlog/refs/heads/release/4.4"
msys2_references=(
  "cpe: cpe:/a:ffmpeg:ffmpeg"
)
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-aom"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${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"
         $([[ ${CARCH} == 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}-rav1e"
         "${MINGW_PACKAGE_PREFIX}-rtmpdump"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-speex"
         "${MINGW_PACKAGE_PREFIX}-srt"
         "${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"
         "${MINGW_PACKAGE_PREFIX}-zvbi")
conflicts=("${MINGW_PACKAGE_PREFIX}-ffmpeg<5")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-dlfcn"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             $([[ ${CARCH} == aarch64 ]] || echo \
               "${MINGW_PACKAGE_PREFIX}-amf-headers" \
               "${MINGW_PACKAGE_PREFIX}-nasm"))
source=(https://ffmpeg.org/releases/${_realname}-${pkgver}.tar.xz{,.asc}
        https://github.com/FFmpeg/FFmpeg/commit/c6fdbe26ef30fff817581e5ed6e078d96111248a.patch
        # 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"
        https://github.com/FFmpeg/FFmpeg/commit/f9620d74cd49c35223304ba41e28be6144e45783.patch
        https://github.com/FFmpeg/FFmpeg/commit/03823ac0c6a38bd6ba972539e3203a592579792f.patch
        https://github.com/FFmpeg/FFmpeg/commit/d2b46c1ef768bc31ba9180f6d469d5b8be677500.patch
        https://github.com/FFmpeg/FFmpeg/commit/d1ed5c06e3edc5f2b5f3664c80121fa55b0baa95.patch)
validpgpkeys=('FCF986EA15E6E293A5644F10B4322F04D67658D8')
sha256sums=('2290461f467c08ab801731ed412d8e724a5511d6c33173654bd9c1d2e25d0617'
            'SKIP'
            '8de70945eeb6a998b9188494a242ba06847b475b9674fcfec48436d99fcbea7f'
            'b19edf64c3daff6acebeb2e45be4e13c5ee035fd24ba1bf9e77a5376efe83628'
            '84b9fcaa188eef15201a105a44c53d647e4fb800a5032336e0948d6bed2cbc1b'
            '06481611d3449e2cc4f4759d2e0ad5b1ce2c4f313d0b63d4cb0a51d9fe02a424'
            '17df7ec458c7c04f365b3a00328abb3a7cf7f9a7ba9cecf0e492be116d7e6277'
            '2d90ce59ca85ef42e02b0e707080a6b1447783c035f48a7103d71c4d691a4dcf'
            '24cbefad7b2ce3b813ef36a36b984e631c579d43b4107baf5d198fb9da23412b'
            '50af862d25bbeddd0d2e3eff7ebe25899651ee24cc23ed6f785180b5523d246e')

# 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() {
  cd "${srcdir}/${_realname}-${pkgver}"

  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 \
    03823ac0c6a38bd6ba972539e3203a592579792f.patch \
    d2b46c1ef768bc31ba9180f6d469d5b8be677500.patch \
    f9620d74cd49c35223304ba41e28be6144e45783.patch

  # svt-av1 3.0 support
  apply_patch_with_msg \
    d1ed5c06e3edc5f2b5f3664c80121fa55b0baa95.patch
}

build() {
  local -a common_config
  common_config+=(
    --incdir="${MINGW_PREFIX}/include/ffmpeg4.4" \
    --libdir="${MINGW_PREFIX}/lib/ffmpeg4.4" \
    --disable-debug
    --disable-stripping
    --disable-doc
    --disable-nvenc
    --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-libsvtav1
    --enable-libtheora
    --enable-libvidstab
    --enable-libvorbis
    --enable-libx264
    --enable-libx265
    --enable-libxvid
    --enable-libvpx
    --enable-libwebp
    --enable-libxml2
    --enable-libzimg
    --enable-libzvbi
    --enable-openal
    --enable-pic
    --enable-postproc
    --enable-runtime-cpudetect
    --enable-swresample
    --enable-version3
    --enable-vulkan
    --enable-zlib
    --enable-librav1e
  )

  if [[ "${CARCH}" != aarch64 ]]; then
    common_config+=(
        --enable-libmfx
        --enable-amf
    )
  fi

  for _variant in -static -shared; do
    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}

    make
  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 || 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" {} \;

  # https://github.com/pkgconf/pkgconf/issues/286
  find "${pkgdir}"${MINGW_PREFIX}/lib/ffmpeg4.4/pkgconfig -name *.pc -exec sed -i -e"s|libdir=${MINGW_PREFIX}/|libdir=${MINGW_PREFIX}/../|g" {} \;
  find "${pkgdir}"${MINGW_PREFIX}/lib/ffmpeg4.4/pkgconfig -name *.pc -exec sed -i -e"s|includedir=${MINGW_PREFIX}/|includedir=${MINGW_PREFIX}/../|g" {} \;
}
