# Description: Complete solution to record, convert and stream audio and video.
# URL: https://ffmpeg.org/
# Maintainer: Danny Rawlins, crux at romster dot me
# Depends on: yasm
# Optional: alsa-lib cuda dav1d fdk-aac nv-codec-headers gnutls gsm jack jack-audio-connection-kit lame libass libsdl2 libtheora libva libvdpau libvorbis libvpx libwebp opus pulseaudio x264 x265

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

build() {
	cd $name-$version

	patch -p1 -i $SRC/vmaf-model-path.patch

	# https://crbug.com/1251779
	patch -p1 -i $SRC/add-av_stream_get_first_dts-for-chromium.patch

[[ -e '/usr/bin/clang' && -e '/usr/lib/pkgconfig/cuda.pc' && -e '/usr/share/pkgconfig/ffnvcodec.pc' ]] && \
	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"

[ -e '/usr/share/pkgconfig/ffnvcodec.pc' ] && PKGMK_FFMPEG+=' --enable-nvdec --enable-nvenc'
[ -e '/usr/include/AMF/core/VulkanAMF.h' ] && PKGMK_FFMPEG+=' --enable-amf'
[ -e '/usr/lib/pkgconfig/libwebp.pc' ] && PKGMK_FFMPEG+=' --enable-libwebp'
[ -e '/usr/lib/pkgconfig/vdpau.pc' ] && PKGMK_FFMPEG+=' --enable-vdpau'
[ -e '/usr/lib/pkgconfig/freetype2.pc' ] && PKGMK_FFMPEG+=' --enable-libfreetype'
[ -e '/usr/lib/pkgconfig/jack.pc' ] && PKGMK_FFMPEG+=' --enable-libjack'
[ -e '/usr/lib/pkgconfig/x264.pc' ] && PKGMK_FFMPEG+=' --enable-libx264'
[ -e '/usr/lib/pkgconfig/x265.pc' ] && PKGMK_FFMPEG+=' --enable-libx265'
[ -e '/usr/lib/pkgconfig/libass.pc' ] && PKGMK_FFMPEG+=' --enable-libass'
[ -e '/usr/lib/libgsm.so' ] && PKGMK_FFMPEG+=' --enable-libgsm'
[ -e '/usr/include/lame/lame.h' ] && PKGMK_FFMPEG+=' --enable-libmp3lame'
[ -e '/usr/lib/pkgconfig/opus.pc' ] && PKGMK_FFMPEG+=' --enable-libopus'
[ -e '/usr/lib/pkgconfig/vpx.pc' ] && PKGMK_FFMPEG+=' --enable-libvpx'
[ -e '/usr/lib/pkgconfig/vorbis.pc' ] && PKGMK_FFMPEG+=' --enable-libvorbis'
[ -e '/usr/lib/pkgconfig/theora.pc' ] && PKGMK_FFMPEG+=' --enable-libtheora'
[ -e '/usr/lib/pkgconfig/libpulse.pc' ] && PKGMK_FFMPEG+=' --enable-libpulse'
[ -e '/usr/lib/pkgconfig/fdk-aac.pc' ] && PKGMK_FFMPEG+=' --enable-libfdk-aac --enable-nonfree'
[ -e '/usr/lib/pkgconfig/dav1d.pc' ] && PKGMK_FFMPEG+=' --enable-libdav1d'

	if [ -e '/usr/lib/pkgconfig/gnutls.pc' ]; then
		PKGMK_FFMPEG+=' --enable-gnutls'
	else
		PKGMK_FFMPEG+=' --enable-openssl --enable-nonfree'
	fi

	./configure ${PKGMK_FFMPEG} \
		--prefix=/usr \
		--disable-debug \
		--disable-doc \
		--enable-gpl \
		--enable-version3 \
		--enable-shared \
		--enable-pic \
		--enable-ffplay \
		--enable-avfilter \
		--enable-postproc \
		--enable-pthreads \
		--enable-swresample

	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
}