# Maintainer : Daniel Bermond <dbermond@archlinux.org>
# Contributor: Iacopo Isimbaldi <isiachi@rhye.it>

pkgname=ffmpeg-full
pkgver=8.1.2
pkgrel=1.1
_whispercpp_ver='1.9.1'
pkgdesc='Complete solution to record, convert and stream audio and video (all possible features including libfdk-aac)'
arch=('x86_64')
url='https://ffmpeg.org/'
license=('LicenseRef-nonfree-and-unredistributable')
depends=(
    'alsa-lib'
    'aom'
    'bzip2'
    'cairo'
    'chromaprint-fftw'
    'dav1d'
    'fontconfig'
    'freetype2'
    'frei0r-plugins' # loaded on-demand by dlopen()
    'fribidi'
    'glib2'
    'glibc'
    'glslang'
    'gnutls'
    'harfbuzz'
    'jack'
    'ladspa' # loaded on-demand by dlopen()
    'lame'
    'lcms2'
    'libass'
    'libbluray'
    'libcdio-paranoia'
    'libdrm'
    'libdvdnav'
    'libdvdread'
    'libfdk-aac'
    'libgcc'
    'libgcrypt'
    'libgomp'
    'libjxl'
    'libmodplug'
    'libmysofa'
    'libopenmpt'
    'libplacebo'
    'libpulse'
    'librist'
    'librsvg'
    'libsoxr'
    'libssh'
    'libstdc++'
    'libtheora'
    'libva'
    'libvdpau'
    'libvorbis'
    'libvpl'
    'libvpx'
    'libx11'
    'libxcb'
    'libxext'
    'libxml2'
    'libxv'
    'libwebp'
    'lilv'
    'ocl-icd'
    'opencolorio'
    'opencv'
    'openh264'
    'openjpeg2'
    'opus'
    'rav1e'
    'rtmpdump'
    'rubberband'
    'sdl2'
    'snappy'
    'sndio'
    'speex'
    'srt'
    'svt-av1'
    'v4l-utils'
    'vapoursynth' # loaded on-demand by dlopen()
    'vid.stab'
    'vmaf'
    'vulkan-icd-loader' # loaded on-demand by dlopen()
    'vvenc'
    'x264'
    'x265'
    'xvidcore'
    'xz'
    'zimg'
    'zlib')
optdepends=(
    'cuda: for CUDA runtime support (CUDART/cuBLAS)'
    'nvidia-utils: for NVIDIA CUVID/NVDEC/NVENC support'
    'vpl-runtime: for Intel Quick Sync Video')
makedepends=(
    'amf-headers'
    'clang'
    'cmake'
    'cuda'
    'ffnvcodec-headers'
    'gcc15'
    'git'
    'gmp'
    'libgl'
    'lv2'
    'nasm'
    'opencl-headers'
    'vulkan-headers')
provides=(
    'ffmpeg'
    'libavcodec.so'
    'libavdevice.so'
    'libavfilter.so'
    'libavformat.so'
    'libavutil.so'
    'libswscale.so'
    'libswresample.so')
conflicts=('ffmpeg')
source=("https://ffmpeg.org/releases/ffmpeg-${pkgver}.tar.xz"{,.asc}
        'git+https://github.com/lensfun/lensfun.git'
        "https://github.com/ggml-org/whisper.cpp/archive/v${_whispercpp_ver}/whisper.cpp-${_whispercpp_ver}.tar.gz"
        '040-ffmpeg-add-av_stream_get_first_dts-for-chromium.patch'
        '050-ffmpeg-fix-cuda-nvcc-with-gcc14.patch'
        '060-ffmpeg-whisper.cpp-fix-pkgconfig.patch'
        'LICENSE')
sha256sums=('464beb5e7bf0c311e68b45ae2f04e9cc2af88851abb4082231742a74d97b524c'
            'SKIP'
            'SKIP'
            '147267177eef7b22ec3d2476dd514d1b12e160e176230b740e3d1bd600118447'
            '95223dda645c15b3daf79cd4d55df5d4ac46207f749973396bb761b743586ed6'
            '1bbd783da8483e2cffe99715125dddbd88c81ae36f28eee5ae7df5705c448077'
            '8d834668c277071dabe91db2a38bf2c02cd73cc5be0ceeb0ed1e176ab9525c68'
            '04a7176400907fd7db0d69116b99de49e582a6e176b3bfb36a03e50a4cb26a36')
validpgpkeys=('FCF986EA15E6E293A5644F10B4322F04D67658D8')

prepare() {
    patch -d "ffmpeg-${pkgver}" -Np1 -i "${srcdir}/040-ffmpeg-add-av_stream_get_first_dts-for-chromium.patch"
    patch -d "ffmpeg-${pkgver}" -Np1 -i "${srcdir}/050-ffmpeg-fix-cuda-nvcc-with-gcc14.patch"
    patch -d "whisper.cpp-${_whispercpp_ver}" -Np1 -i "${srcdir}/060-ffmpeg-whisper.cpp-fix-pkgconfig.patch"
}

build() {
    local _stagingdir="${srcdir}/staging"
    local _pkgconfigdir="${_stagingdir}/lib/pkgconfig"
    export PKG_CONFIG_PATH="${_pkgconfigdir}${PKG_CONFIG_PATH:+":${PKG_CONFIG_PATH}"}"

    export CC=gcc-15
    export CXX=g++-15

    local -a _cmake_opts=(
        '-GUnix Makefiles'
        '-DBUILD_SHARED_LIBS:BOOL=OFF'
        '-DCMAKE_BUILD_TYPE:STRING=None'
        "-DCMAKE_INSTALL_PREFIX:PATH=${_stagingdir}"
        '-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON'
        '-DCMAKE_C_COMPILER=gcc-15'
        '-DCMAKE_CXX_COMPILER=g++-15'
        '-Wno-dev')

    # ffmpeg requires lensfun git master, but lensfun-git package wrongly installs its files to non-standard locations:
    # https://aur.archlinux.org/cgit/aur.git/commit/?h=lensfun-git&id=7b7a2d4890df59cde62c7dbfde3cefd7868a2707
    # building it locally as a static library for the time being - this also have the benefit of avoid rebuilding packages
    # requiring lensfun, like gegl (required for gimp, a commonly used package), as lensfun git master have a soname bump
    cmake -B build/lensfun -S lensfun \
        "${_cmake_opts[@]}" \
        -DBUILD_STATIC:BOOL='ON' \
        -DINSTALL_PYTHON_MODULE:BOOL='OFF' \
        -DINSTALL_HELPER_SCRIPTS:BOOL='OFF'
    cmake --build build/lensfun --target install
    sed -i \
        -e 's/\(-llensfun\)/\1 -lglib-2.0 -lstdc++/' \
        -e '/Cflags: /s/$/ -DCONF_LENSFUN_STATIC/' "${_pkgconfigdir}/lensfun.pc"

    # whisper.cpp AUR package conflicts with imagemagick at the time of writing
    # building it locally as a static library for the time being, as imagemagick is a commonly used package (high usage in pkgstats)
    cmake -B build/whisper.cpp -S "whisper.cpp-${_whispercpp_ver}" \
        "${_cmake_opts[@]}" \
        -DWHISPER_BUILD_EXAMPLES:BOOL='OFF' \
        -DWHISPER_BUILD_TESTS:BOOL='OFF' \
        -DGGML_CUDA:BOOL='ON' \
        -DCMAKE_CUDA_ARCHITECTURES='89' \
        -DCMAKE_CUDA_HOST_COMPILER=g++-15
    cmake --build build/whisper.cpp --target install

    cd "ffmpeg-${pkgver}"
    printf '%s\n' '  -> Running ffmpeg configure script...'

    export CFLAGS+=' -isystem/opt/cuda/include'
    export LDFLAGS+=' -L/opt/cuda/lib64'

    ./configure \
        --prefix='/usr' \
        --cc=gcc-15 \
        --cxx=g++-15 \
        --enable-lto \
        \
        --disable-rpath \
        --enable-gpl \
        --enable-version3 \
        --enable-nonfree \
        --enable-shared \
        --disable-static \
        --disable-stripping \
        --disable-htmlpages \
        --enable-gray \
        \
        --enable-alsa \
        --enable-bzlib \
        --enable-cairo \
        --enable-chromaprint \
        --enable-frei0r \
        --enable-gcrypt \
        --enable-gmp \
        --enable-gnutls \
        --enable-iconv \
        --enable-ladspa \
        --enable-lcms2 \
        --enable-libaom \
        --enable-libass \
        --enable-libbluray \
        --enable-libcdio \
        --enable-libdav1d \
        --enable-libdvdnav \
        --enable-libdvdread \
        --enable-libfdk-aac \
        --enable-libfontconfig \
        --enable-libfreetype \
        --enable-libfribidi \
        --enable-libglslang \
        --enable-libharfbuzz \
        --enable-libjack \
        --enable-libjxl \
        --enable-liblensfun \
        --enable-libmodplug \
        --enable-libmp3lame \
        --enable-libopencolorio \
        --enable-libopencv \
        --enable-libopenh264 \
        --enable-libopenjpeg \
        --enable-libopenmpt \
        --enable-libopus \
        --enable-libplacebo \
        --enable-libpulse \
        --enable-librav1e \
        --enable-librist \
        --enable-librsvg \
        --enable-librubberband \
        --enable-librtmp  \
        --disable-libshaderc \
        --enable-libsnappy \
        --enable-sndio \
        --enable-libsoxr \
        --enable-libspeex \
        --enable-libsrt \
        --enable-libssh \
        --enable-libsvtav1 \
        --disable-libtensorflow \
        --enable-libtheora \
        --disable-libtls \
        --disable-libtorch \
        --enable-libv4l2 \
        --enable-libvidstab \
        --enable-libvmaf \
        --enable-libvorbis \
        --enable-libvpx \
        --enable-libvvenc \
        --enable-libwebp \
        --enable-libx264 \
        --enable-libx265 \
        --enable-libxcb \
        --enable-libxcb-shm \
        --enable-libxcb-xfixes \
        --enable-libxcb-shape \
        --enable-libxvid \
        --enable-libxml2 \
        --enable-libzimg \
        --enable-lv2 \
        --enable-lzma \
        --disable-mbedtls \
        --enable-libmysofa \
        --disable-openssl \
        --disable-pocketsphinx \
        --enable-vapoursynth \
        --enable-whisper \
        --enable-xlib \
        --enable-zlib \
        \
        --enable-sdl2 \
        \
        --enable-amf \
        --enable-cuda-nvcc \
        --enable-cuda-llvm \
        --enable-cuvid \
        --enable-ffnvcodec \
        --enable-libdrm \
        --enable-libvpl \
        --disable-libnpp \
        --enable-nvdec \
        --enable-nvenc \
        --disable-ohcodec \
        --enable-opencl \
        --enable-opengl \
        --enable-vdpau \
        --enable-vaapi \
        --enable-vulkan
    make
    make tools/qt-faststart
}

package() {
    make -C "ffmpeg-${pkgver}" DESTDIR="$pkgdir" install
    install -D -m755 "ffmpeg-${pkgver}/tools/qt-faststart" -t "${pkgdir}/usr/bin"
    install -D -m644 LICENSE -t "${pkgdir}/usr/share/licenses/${pkgname}"
    sed -i \
        -e 's|-llensfun ||' \
        -e 's|-lwhisper ||' \
        -e "s|-L${srcdir}/staging/lib ||g" \
        "${pkgdir}/usr/lib/pkgconfig/libavfilter.pc"
}
