# Description: Complete solution to record, convert and stream audio and video.
# URL: https://ffmpeg.org/
# Maintainer: Tim Biermann, tbier at posteo dot de
# Depends on: yasm
# Optional: alsa-lib amf-headers aom chromaprint cuda dav1d fdk-aac fontconfig freetype gnutls gsm jack jack-audio-connection-kit ladspa lame lcms2 lensfun libass libbs2b libcaca libcdio-paranoia libdrm libmodplug libplacebo librsvg libsdl2 libsoxr libssh libtheora libva libvdpau libvorbis libvpx libwebp libxml2 lv2 nv-codec-headers openal opencv openh264 openjpeg2 opus pulseaudio rav1e rtmpdump rubberband shaderc snappy speex svt-av1 v4l-utils x264 x265

name=ffmpeg
version=8.1.2
release=1
source=(https://ffmpeg.org/releases/$name-$version.tar.bz2
  add-av_stream_get_first_dts-for-chromium.patch
  ffmpeg-x264-10bit.sh)

build() {
  cd $name-$version

  prt-get isinst clang cuda nv-codec-headers && \
    PKGMK_FFMPEG+=' --enable-cuda-llvm --enable-nonfree --enable-cuda-nvcc --enable-libnpp' && \
    PKGMK_FFMPEG+=' --extra-cflags=-I/opt/cuda/include --extra-ldflags=-L/opt/cuda/lib64' && \
    PATH="$PATH:/opt/cuda/bin"

  prt-get isinst amf-headers && PKGMK_FFMPEG+=' --enable-amf'
  prt-get isinst aom && PKGMK_FFMPEG+=' --enable-libaom'
  prt-get isinst chromaprint && PKGMK_FFMPEG+=' --enable-chromaprint'
  prt-get isinst dav1d && PKGMK_FFMPEG+=' --enable-libdav1d'
  prt-get isinst fdk-aac && PKGMK_FFMPEG+=' --enable-libfdk-aac --enable-nonfree'
  prt-get isinst fontconfig && PKGMK_FFMPEG+=' --enable-fontconfig'
  prt-get isinst freetype && PKGMK_FFMPEG+=' --enable-libfreetype'
  prt-get isinst gnutls && PKGMK_FFMPEG+=' --enable-gnutls' || PKGMK_FFMPEG+=' --enable-openssl --enable-nonfree'
  prt-get isinst gsm && PKGMK_FFMPEG+=' --enable-libgsm'
  prt-get isinst jack && PKGMK_FFMPEG+=' --enable-libjack'
  prt-get isinst ladspa && PKGMK_FFMPEG+=' --enable-ladspa'
  prt-get isinst lame && PKGMK_FFMPEG+=' --enable-libmp3lame'
  prt-get isinst lcms2 && PKGMK_FFMPEG+=' --enable-lcms2'
  #prt-get isinst lensfun && PKGMK_FFMPEG+=' --enable-liblensfun'
  prt-get isinst libass && PKGMK_FFMPEG+=' --enable-libass'
  prt-get isinst libbs2b && PKGMK_FFMPEG+=' --enable-libbs2b'
  prt-get isinst libcaca && PKGMK_FFMPEG+=' --enable-libcaca'
  prt-get isinst libcdio-paranoia && PKGMK_FFMPEG+=' --enable-libcdio'
  prt-get isinst libdrm && PKGMK_FFMPEG+=' --enable-libdrm'
  prt-get isinst libmodplug && PKGMK_FFMPEG+=' --enable-libmodplug'
  prt-get isinst libplacebo && PKGMK_FFMPEG+=' --enable-libplacebo'
  prt-get isinst librsvg && PKGMK_FFMPEG+=' --enable-librsvg'
  prt-get isinst libsoxr && PKGMK_FFMPEG+=' --enable-libsoxr'
  prt-get isinst libssh && PKGMK_FFMPEG+=' --enable-libssh'
  prt-get isinst libtheora && PKGMK_FFMPEG+=' --enable-libtheora'
  prt-get isinst libvdpau && PKGMK_FFMPEG+=' --enable-vdpau'
  prt-get isinst libvorbis && PKGMK_FFMPEG+=' --enable-libvorbis'
  prt-get isinst libvpx && PKGMK_FFMPEG+=' --enable-libvpx'
  prt-get isinst libwebp && PKGMK_FFMPEG+=' --enable-libwebp'
  prt-get isinst libxml2 && PKGMK_FFMPEG+=' --enable-libxml2'
  prt-get isinst lv2 && PKGMK_FFMPEG+=' --enable-lv2'
  prt-get isinst nv-codec-headers && PKGMK_FFMPEG+=' --enable-nvdec --enable-nvenc'
  prt-get isinst opus && PKGMK_FFMPEG+=' --enable-libopus'
  prt-get isinst openh264 && PKGMK_FFMPEG+=' --enable-libopenh264'
  prt-get isinst openjpeg2 && PKGMK_FFMPEG+=' --enable-libopenjpeg'
  prt-get isinst openal && PKGMK_FFMPEG+=' --enable-openal'
  #prt-get isinst opencv && PKGMK_FFMPEG+=' --enable-libopencv'
  prt-get isinst pulseaudio && PKGMK_FFMPEG+=' --enable-libpulse'
  prt-get isinst rtmpdump && PKGMK_FFMPEG+=' --enable-librtmp'
  prt-get isinst rav1e && PKGMK_FFMPEG+=' --enable-librav1e'
  prt-get isinst rubberband && PKGMK_FFMPEG+=' --enable-librubberband'
  prt-get isinst shaderc && PKGMK_FFMPEG+=' --enable-libshaderc'
  prt-get isinst snappy && PKGMK_FFMPEG+=' --enable-libsnappy'
  prt-get isinst speex && PKGMK_FFMPEG+=' --enable-libspeex'
  prt-get isinst svt-av1 && PKGMK_FFMPEG+=' --enable-libsvtav1'
  #prt-get isinst v4l-utils && PKGMK_FFMPEG+=' --enable-libv4l2'
  prt-get isinst x264 && PKGMK_FFMPEG+=' --enable-libx264'
  prt-get isinst x265 && PKGMK_FFMPEG+=' --enable-libx265'

  patch -Np1 -i $SRC/add-av_stream_get_first_dts-for-chromium.patch

  sed -i -e '/check_cflags -fdiagnostics-color=auto/d' configure

  export CFLAGS="$CFLAGS -ffat-lto-objects"
  export CXXFLAGS="$CXXFLAGS -ffat-lto-objects"

  ./configure ${PKGMK_FFMPEG} \
    --prefix=/usr \
    --optflags="$CFLAGS" \
    --cc=/usr/bin/gcc \
    --cxx=/usr/bin/g++ \
    --ar=/usr/bin/gcc-ar \
    --nm=/usr/bin/gcc-nm \
    --ranlib=/usr/bin/gcc-ranlib \
    --strip=/usr/bin/strip \
    --enable-gpl \
    --enable-version3 \
    --enable-shared \
    --enable-pic \
    --enable-lto \
    --enable-ffplay \
    --enable-avfilter \
    --enable-pthreads \
    --enable-swresample \
    --disable-debug \
    --disable-doc \
    --disable-libcelt \
    --disable-stripping

  make
  make DESTDIR=$PKG install install-man

  rm -r $PKG/usr/share/ffmpeg/examples

  if [ -e '/usr/lib/pkgconfig/x264.pc' ]; then
    install -m 0755 -D $SRC/ffmpeg-x264-10bit.sh \
      $PKG/usr/bin/ffmpeg-x264-10bit
  fi
}
